使用JQuery获取复选框选中状态

时间:2016-08-08 07:08:24

标签: javascript jquery checkbox

我尝试了很多方法来获取复选框的选定状态。它是带有类型复选框的输入字段。

我尝试了以下方法来获取状态,但都返回false。只有在之前使用jQuery选中/未选中复选框时(这是使用$.prop("checked", "true")完成的。

$("#ID").prop("checked");
$("#ID").val(); //returns "on" everytime
$("#ID").attr("checked");
$("#ID").checked; //undefined

ID是我的代码中的真实ID!

选择的状态也是用jQuery设置的:

$(columns[i]).children().first().prop("checked", stateArray[i]);

复选框位于KendoGrid中,columns.children获取所有td' s,first()返回输入。

stateArray是一个带有true / false的数组。

什么都行不通......你有个主意吗?

此致

编辑:我发现了错误......为输入生成的ID总是一样的...所以它选择了第一个未被选中的元素......我很抱歉!

3 个答案:

答案 0 :(得分:1)

你的选择器在哪里?像下面一样使用is(":checked")

var status = $(':checkbox').is(":checked");

答案 1 :(得分:0)

要查找复选框,请使用$('input[type="checkbox"]').prop("checked")

有关详情,请查看以下代码段。



$('input[type="checkbox"]').on('change',function(){
  if($(this).prop('checked')){
    alert("checked");
  }else{
    alert("un-checked");  
  }
});

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="checkbox"/>
&#13;
&#13;
&#13;

答案 2 :(得分:0)

您需要先从DOM选择checked,然后阅读其$(function() { console.log($('#ab3fee6b-c81e-e4ba-b12e-2c088bb386cc_A4').prop('checked')); console.log($('#ab3fee6b-c81e-e4ba-b12e-2c088bb386cc_A5').prop('checked')); });属性。

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<input id="ab3fee6b-c81e-e4ba-b12e-2c088bb386cc_A4" type="checkbox" data-role="check" otg-visible="true" style="-webkit-appearance: checkbox !important; width: 20px; height: 20px; display: table-cell; margin: 0px;">
<input id="ab3fee6b-c81e-e4ba-b12e-2c088bb386cc_A5" type="checkbox" data-role="check" checked otg-visible="true" style="-webkit-appearance: checkbox !important; width: 20px; height: 20px; display: table-cell; margin: 0px;">
add()
{
    unsigned char a[67] = {0};
    for( int i=0; i<67; i++)
        a[i] = a[i] + 4;
}