【问题标题】:Disabling check box and uncheck it using jQuery禁用复选框并使用 jQuery 取消选中它
【发布时间】:2014-01-14 08:01:03
【问题描述】:

在下面的代码中,我在 for 循环中动态生成 td 元素。

jQuery("#dialog_load_content").load(url, function() {
        var clientName = jQuery('#client option:selected').text();
        var clientId = Number(jQuery('#client option').filter(function() {return jQuery(this).html() == clientName;}).val());
        var navDate = jQuery('input:checkbox:checked.signOff').closest('tr').find('td:eq(2)').html();
        var fundName = jQuery('input:checkbox:checked.signOff').closest('tr').find('td:eq(0)').html();
        var fundId = Number(jQuery('#fund option').filter(function() {return jQuery(this).html() == fundName;}).val());
        jQuery.post('getNavPackReportsStatus', {clientId: clientId, fundId: fundId, periodEndDate: navDate}, function(data) {
            var reports = data;

            for(var count = 0; count< reports.length; count++) {
                jQuery('#wrkbkRptTable tbody').append('<tr>' +
                        '<td><input type="checkbox" id="'+reports[count].reportStoreId+'" name="'+reports[count].reportName+'" checked/></td>'+
                        '<td>' + (count + 1) + '</td>'+
                        '<td>' + reports[count].reportGroupDisplayName + '</td>'+
                        '<td>' + reports[count].reportName + '</td>'+
                        '<td id="chkReportID">' + ((reports[count].reportStoreId == null || reports[count].reportStoreId == '') ? '<font color="red">Not Available</font>' : '<font color="green">Available</font>') + '</td>'+
                        '</tr>');
            }


        });

    });

我尝试使用此禁用复选框并取消选中复选框,但它不起作用

 jQuery('#wrkbkRptTable input:checked').each(function() {
     var test=jQuery('#wrkbkRptTable input:checked').attr('id');
    if(test==null || test=='' || test=='undefined')
    {
         alert(test);
         jQuery("#"+test+"").prop( "disabled", true );
    }

});    

我想禁用复选框并使用第一个 td(id 属性值)取消选中它,例如 this: if (id == null then disable & uncheck it)

【问题讨论】:

  • 你用的是哪个版本的jquery?
  • jQuery 1.8.3 版本

标签: javascript html jquery checkbox


【解决方案1】:

试试这个

jQuery.each( jQuery('#wrkbkRptTable input:checked'), function(_, item) {
  var item = jQuery(item);
  var id = item.attr('id');
  if (typeof id == 'undefined') {
    item.attr('checked', false);
    item.attr('disabled', true);
  }
} )

此代码将收到所有选中的复选框。然后它将测试项目的 ID 是否存在。如果没有,它将取消选中当前复选框并禁用它。

【讨论】:

  • 尝试了建议的代码,但它不工作也没有给出任何错误
  • 尝试在 if 条件前添加 console.log(item) 并将结果粘贴到评论中。
  • Object[输入属性值=“on”属性值=“null”]0输入属性值=“on”属性值=“null”上下文输入属性值=“on”属性值=” null" 长度 1 goNavPack.js (line 757) Object[input#e74bd50f08bfdd88156c1fd1b169154a property value = "on" attribute value = "null"] 0 input#e74bd50f08bfdd88156c1fd1b169154a property value = "on" attribute value = "null" context input#e74bd50f0b8 value = "on" 属性 value = "null" 长度 1
【解决方案2】:

试试这样的

$(document).ready(function() {
     jQuery('#wrkbkRptTable input:checked').each(function() {
        var test = this.id;
        if(test==null || test=='undefined' || test.trim()=='')
        {
             this.checked = false;
             this.disabled = true;
        }
    });
});

【讨论】:

    【解决方案3】:

    使用firebug aor chrome debugger来调试这个。你的禁用复选框的代码是对的。

    【讨论】:

    • 是的,它正在禁用,但都不是想要的......也没有给出错误
    • 使用class jQuery("."+test+"").prop("disabled", true);代替ID
    【解决方案4】:

    我猜 if 中的条件不太合适

    if(test==null || test=='' || test=='undefined')
    {
        // means not got test 
    }
    

    试试这个

    if(test)
    {
        // ur codes 
    }
    

    【讨论】:

      【解决方案5】:
       jQuery('#wrkbkRptTable input:checked').each(function() {
       var test=jQuery(this).attr('id');
        if(!test)
          {
            alert(test);
            jQuery(this).prop( "disabled", true );
           }
      
        });
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-06-24
        • 1970-01-01
        • 2013-08-04
        • 2013-11-26
        • 1970-01-01
        • 2014-09-28
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多