【问题标题】:jQuery Disable Multiple Dropdowns Not WorkingjQuery禁用多个下拉菜单不起作用
【发布时间】:2012-03-12 16:52:58
【问题描述】:

我有供商家输入营业时间的下拉菜单。每天都有一个 ID 为 hours_dayname_open 和 hours_dayname_closed 的下拉列表。我还有一个复选框将其标记为已关闭。如果选中,我正在使用以下 jQuery 禁用下拉菜单:

$("#closed_monday").click( function(){
    if($(this).is(':checked')){
        $("#hours_monday_open").attr("disabled", true);
        $("#hours_monday_closed").attr("disabled", true);
    }else{
        $("#hours_monday_open").attr("disabled", false);
        $("#hours_monday_closed").attr("disabled", false);
    }
});

但是,当复选框被选中时,只有开放时间被禁用/启用...关闭的下拉菜单似乎被忽略了。

【问题讨论】:

  • 如果您使用的是 1.7.+,您可能需要查看 prop() 方法
  • 不幸的是,我现在被 1.4.2 困住了

标签: jquery html


【解决方案1】:

奇怪的是,正确的属性是disabled,而不是true

$("#hours_monday_open").attr("disabled", "disabled");

要启用它,请删除 disabled 属性:

$("#hours_monday_open").removeAttribute("disabled");

从 jQuery 1.6 开始,您可以使用 .prop() 功能来清除/设置它。

【讨论】:

    【解决方案2】:
        $("#closed_monday").click( function(){
          if($(this).is(':checked')){
            $("#hours_monday_open, #hours_monday_closed").attr("disabled", "disabled");
          }else{
            $("#hours_monday_open, #hours_monday_closed").removeAttribute("disabled");
          }
        });​
    

    【讨论】:

      【解决方案3】:
      $("#closed_monday").change( function(){
          if($(this).is(':checked')){
              $("#hours_monday_open").attr("disabled", "disabled");
              $("#hours_monday_closed").attr("disabled", "disabled");
          }else{
              $("#hours_monday_open").removeAttribute("disabled");
              $("#hours_monday_closed").removeAttribute("disabled");
          }
      });
      

      就是对.change()和.click()的简单改动,只是错误的事件。

      http://jsfiddle.net/6Yr8Q/2/

      【讨论】:

        猜你喜欢
        • 2013-12-22
        • 1970-01-01
        • 2016-04-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-11-24
        • 2016-02-17
        • 1970-01-01
        相关资源
        最近更新 更多