gpt4 book ai didi

javascript - WordPress 中的本地化 jQuery 确认按钮

转载 作者:行者123 更新时间:2023-12-01 02:27:11 29 4
gpt4 key购买 nike

我想本地化 WordPress 中的 jQuery 确认按钮。为此,我在 PHP 文件中编码如下:

function Confirmation()
{
// Register the script
wp_register_script('eux-conf', '/js/eux-js.js');
// Localize the script with new data
$translation_array = array(
'confirmation' => __('Confirmation', 'eux-loc'),
'message' => __('Do you want to delete this item?', 'eux-loc'),
'yes' => __('Yes', 'eux-loc'),
'no' => __('No', 'eux-loc')
);
// Enqueued script with localized data.
wp_enqueue_script('eux-conf');
wp_localize_script('eux-conf', 'meta', $translation_array);
}
add_action('admin_print_scripts', 'Confirmation');

和 jQuery 代码:

jQuery('.delete').click(function()
{
jQuery.confirm({
'title': meta.confirmation,
'message': meta.message,
buttons: [
{
text: meta.yes,
classes: 'widget btn primary',
id: "yes",
onclick: function() {


}
},
{
text: meta.no,
classes: 'widget btn secondary',
id: "no",
onclick: 'close'
}
]

});

编辑:在 David Lee 回答之后,我意识到,如果我写 "This is a String" 而不是 meta.yes,我再次获取 01,如下所示:

        buttons: [
{
text: "This is a String",
classes: 'widget btn primary',
id: "yes",
onclick: function() {


}
},
{
text: meta.no,
classes: 'widget btn secondary',
id: "no",
onclick: 'close'
}
]

我想首先,我必须纠正我的按钮参数,但我不知道。

但这给了我这样的结果:

enter image description here

你看,我的按钮是,但代码显示01。我该如何纠正这个问题?

最佳答案

尝试

function Confirmation()
{
// Register the script
wp_register_script('eux-conf', '/js/eux-js.js');
// Localize the script with new data
$translation_array = array(
'confirmation' => __('Confirmation', 'eux-loc'),
'message' => __('Do you want to delete this item?', 'eux-loc'),
'yes_s' => __('Yes', 'eux-loc'),
'no_s' => __('No', 'eux-loc')
);
// Enqueued script with localized data.
wp_localize_script('eux-conf', 'meta', $translation_array);//this one first
wp_enqueue_script('eux-conf');

}
add_action('admin_enqueue_scripts', 'Confirmation');

在 jQuery 中:

jQuery('.delete').click(function()
{
jQuery.confirm({
'title': meta.confirmation,
'message': meta.message,
buttons: [
{
text: meta.yes_s,
classes: 'widget btn primary',
id: "yes",
onclick: function() {


}
},
{
text: meta.no_s,
classes: 'widget btn secondary',
id: "no",
onclick: 'close'
}
]

});

我改变了顺序,你需要先本地化然后enqueue,我还将 Hook 更改为admin_enqueue_scripts,并更改了变量的名称,因为no 是保留的。

关于javascript - WordPress 中的本地化 jQuery 确认按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54955204/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com