简体   繁体   English

Jquery 更改功能不起作用 wordpress 管理仪表板

[英]Jquery Change function not working wordpress admin dashboard

I am working on post format select option based in WordPress.我正在研究基于 WordPress 的帖子格式选择选项。 Trying to get the value of the selected option.试图获取所选选项的值。 But change method is not working.但是更改方法不起作用。 Not giving the alert value also the test alert.没有给出警报值也没有给出测试警报。 My code is below,我的代码在下面,

(function (jQuery){
    "use strict";
    jQuery(document).ready(function () {

        jQuery("#post-format-selector-0").change(function () {

            var selectedVal = $("#post-format-selector-0:selected").val();

            alert("Hi, your favorite programming language is " + selectedVal);

            alert("Hi, your favorite programming language is PHP ");

        });

    });
})(jQuery);

在此处输入图片说明

You're using the $ alias on your change function.您在更改函数中使用了$别名。 Try changing your code to:尝试将您的代码更改为:

(function ($){
    "use strict";
    $(document).ready(function () {

        $(document).on('change','#post-format-selector-0',function(){

            var selectedVal = $("#post-format-selector-0:selected").val();

            alert("Hi, your favorite programming language is " + selectedVal);

            alert("Hi, your favorite programming language is PHP ");

        });

    });
})(jQuery);

Edit: If you're enqueuing your script in the header, even though you have redundant ready functions, the select element may not be loaded before the change function is set up.编辑:如果您将脚本放入标题中,即使您有多余的ready函数,在设置change函数之前也可能不会加载select元素。 Assigning the on function to the document (or preferably, the nearest parent) allows it to fire on the select once it eventually shows up.on函数分配给document (或者最好是最近的父document )允许它在最终出现时触发select

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM