jQuery函数没有在页面加载

时间:2018-05-29 11:13:58

标签: jquery

我有以下带有函数的jQuery文件:

jQuery(document).ready(function(){
   // Function to disable first two options of dropdowns
   function disableNG(){
        jQuery('[name=bundle_attribute_pa_maat_163] option:eq(0)').attr("disabled",true);
        jQuery('[name=bundle_attribute_pa_maat_160] option:eq(0)').attr("disabled",true);
        jQuery('[name=bundle_attribute_pa_maat_163] option:eq(1)').attr("disabled",true);
        jQuery('[name=bundle_attribute_pa_maat_160] option:eq(1)').attr("disabled",true);
    };
    disableNG();
});

但由于某种原因,此功能并未禁用页面加载下拉列表的两个第一个选项。

我在一些disableNG();函数上调用函数.change(),它们可以正常工作。

我不明白为什么他们不会在初始页面加载时禁用。

1 个答案:

答案 0 :(得分:1)

首先,使用新的ready state语法。其次,使用.prop代替.attr,因为那是property而不是attribute你要设置的。

jQuery(function($){
   // Function to disable first two options of dropdowns
   function disableNG(){
        $('[name=bundle_attribute_pa_maat_163] option:eq(0)').prop("disabled",true);
        $('[name=bundle_attribute_pa_maat_160] option:eq(0)').prop("disabled",true);
        $('[name=bundle_attribute_pa_maat_163] option:eq(1)').prop("disabled",true);
        $('[name=bundle_attribute_pa_maat_160] option:eq(1)').prop("disabled",true);
    };
    disableNG();
});
相关问题