判定复选框的选中状态


1获取当前状态(.prop())--- 可用来判断状态
<input type="checkbox" name="test" id="test1"/> //未被选中
<input type="checkbox" name="test" id="test2"/ checked>  //选中
console.log($("#test1").prop("checked"))  //打印出false
console.log($("#test2").prop("checked"))  //打印出true
用prop取值,选中状态为true,未选中状态都是false,
所以要判断是否选中,可以使用.prop("checked")
 
 

2$("input[type='checkbox']").is(':checked')---可用来判断状态

<input type="checkbox" name="test" id="test1"/> //未被选中
<input type="checkbox" name="test" id="test2"/ checked>  //选中
console.log($("#test1").is(":checked"))  //打印出false
console.log($("#test2").is(":checked"))  //打印出true
用.is(':checked'),选中状态为true,未选中状态都是false,
所以要判断是否选中,可以使用.is(':checked')


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM