簡體   English   中英

本地化jQuery確認WordPress中的按鈕

[英]Localization jQuery confirm buttons in WordPress

我想在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'
                    }
                ]

我想首先,我必須糾正我的按鈕參數,但我不知道。

但這讓我喜歡這里:

在此輸入圖像描述

你看,我的buttos是YesNo ,但代碼顯示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 ,我也將鈎子更改為admin_enqueue_scripts ,並更改了變量的名稱,因為no是保留的變量。

嘗試,

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

            }
        },
        no: {
             text: meta.no_s, // OR text: "'"+meta.no_s+"'",
             classes: 'widget btn secondary',
             id: "no",
             onclick: 'close'
        },
    }
});

jQuery Confirm只是一個插件而不僅僅是一個插件。 還有一個名為jQuery.confirm的插件。 哪個是你的插件jQuery-ConfirmBootboxJS或其他。 我建議你更新你的插件。 因為Jaydip Nimavat的榜樣有效。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM