获取checkbox选中状态的两种方式
获取checkbox选中状态的两种方式
解决问题:使用jquery框架生成的表格checkbox内容(生成的name、id熟悉什么的jquery获取不到)
第一种方式
使用jquery方式获取状态:
html代码:
<input type="checkbox">
$("[type=checkbox]").click(function () {
alert($(this).is(":checked"))
})
补充其他使用JQuery获取状态的几种方法:
//jquery获取选中的三种方法
$(this).attr('checked); //看版本1.6+返回:”checked”或”undefined”
$(this).prop('checked'); //16+:true/false
$(this).is(':checked'); //所有版本:true/false
$(this).prop("checked","checked");
//query赋值checked的几种写法:
$(this).attr("checked","checked");
$(this).attr("checked",true);
$(this).prop("checked",true);
$(this).prop({checked:true});
$(this).prop("checked",function(){
return true;//函数返回true或false
});
第二种方式
使用原生js click函数:
html代码:
<input type="checkbox" onclick="checkbox(this)">
function checkbox(obj) {
alert(obj.checked);
}
我的其他文章
其他
自己做的小商城,开发技术 SpringCloud + Nacos + 支付宝支付 + Vue
感兴趣的可以相互讨论技术!
顺便说一下,国内又一款开源软件Wall,搭建特别简单,可以搭建个人照片墙和视频墙,有兴趣可以看下教程:https://blog.csdn.net/u014641168/article/details/129396364