如何使用复选框数组的id检查复选框数组中的复选框是否选中?

我正在使用以下代码,但它总是返回选中复选框的计数,而不管id如何。

function isCheckedById(id) {
    alert(id);
    var checked = $("input[@id=" + id + "]:checked").length;
    alert(checked);

    if (checked == 0) {
        return false;
    } else {
        return true;
    }
}

当前回答

$(document).on('click','#checkBoxId',function(){
  var isChecked = $(this).is(':checked');
  console.log(isChecked);
});

上面的代码也适用于引导模式。isChecked为true或flase;

其他回答

$(document).on('click','#checkBoxId',function(){
  var isChecked = $(this).is(':checked');
  console.log(isChecked);
});

上面的代码也适用于引导模式。isChecked为true或flase;

您的问题并不清楚:您希望在输入时给出“checkbox array id”,在输出时得到true/false,这样您就不知道选中了哪个复选框(正如您的函数名所示)。下面是我对isCheckedById主体的建议,它在输入时接受复选框id,在输出时返回true/false(这很简单,但您的id不应该是关键字),

this[id].checked

函数isCheckedById(id){返回此[id].checked;}//测试函数检查(){console.clear()console.log('1',isCheckedById(“myCheckbox1”));console.log('2',isCheckedById(“myCheckbox2”));console.log('3',isCheckedById(“myCheckbox3”));}<label><input id=“myCheckbox1”type=“checkbox”>check 1</label><label><input id=“myCheckbox2”type=“checkbox”>check 2</label><label><input id=“myCheckbox3”type=“checkbox”>check 3</label><!-- 输入周围的标签使文本可单击--><br><button onclick=“check()”>show checked</button>

试试这个。。。

$(function(){
  $('body').on('click','.checkbox',function(e){
    
    if($(this).is(':checked')){
      console.log('Checked')
    } else {
      console.log('Unchecked')
    }
  })
})

你可以试试这个:

<script>
function checkAllCheckBox(value)
{
   if($('#select_all_').is(':checked')){
   $(".check_").attr ( "checked" ,"checked" );
    }
    else
    {
        $(".check_").removeAttr('checked');
    }

 }

</script>
<input type="checkbox" name="chkbox" id="select_all_" value="1" />


<input type="checkbox" name="chkbox" class="check_" value="Apples" />
<input type="checkbox" name="chkbox" class="check_" value="Bananas" />
<input type="checkbox" name="chkbox" class="check_" value="Apples" />
<input type="checkbox" name="chkbox" class="check_" value="Bananas" />
$('#checkbox').is(':checked'); 

如果选中复选框,则上述代码返回true,否则返回false。