jQuery判断checkbox取消勾选时的问题
问题描述:
Jsp:<input type="checkbox" name="selectCode" value='<s:property value="productCode"/>' onclick="selectProductCode('<s:property value="productCode"/>')"/>显示的文本
jQuery:function selectProductCode(code){
alert($("input:checkbox[name='selectCode']:checked").is(':checked'));
};
首先选择了几个复选框,判断结果为true,但是不知道为什么取消勾选时只有最后一个为false,前面取消的尽然也为true,找不出是什么原因..
答
$("input:checkbox[name='selectCode']:checked")这个选取的就是所有被勾选input,只要有勾选的就是true,全部取消勾选后才是false
先搞清楚你的需求。。要是只判断当前对象是否勾选,传递this对象进入selectProduct中
onclick="selectProductCode('<s:property value="productCode"/>',this)"
function selectProductCode(code,cb){
alert(cb.checked);
};
答
"alert($("input:checkbox[name='selectCode']:checked").is(':checked'));"中的name='selectCode',取得是所有name属性为selectCode的元素