简体   繁体   English

jQuery:如何找到前一个复选框被选中

[英]jQuery: how to find that prevous checkbox is checked

I have a code that each checking/unchecking the checkbox respectively adds/subtracts a certain amount.我有一个代码,每个选中/取消选中复选框分别添加/减去一定数量。 Everything works perfectly, but when the first checbox leave "checked" and I press to a second, then the first checbox I "unchecked", but the script didn't subtracts a certain amount.一切正常,但是当第一个复选框离开“选中”并且我按下一秒钟时,然后我“取消选中”第一个复选框,但脚本没有减去一定数量。 I want to create if, that can match previous checbox and present (it is different), and if it's different will take away a certain amount of this first.我想创建 if, that can match the previous checkbox and present (it is different),如果它不同,将首先带走一定数量的 this。 This is my if instruction ( lastClicked.find('input[type=checbox]').attr("checked") doesn't work)这是我的 if 指令( lastClicked.find('input[type=checbox]').attr("checked")不起作用)

if(!((lastClicked.length == $(this).length && lastClicked.length == lastClicked.filter($(this)).length) || lastClicked==="") && lastClicked.find('input[type=checbox]').attr("checked"))

And here is code: https://jsfiddle.net/3p5z6zov/这是代码: https : //jsfiddle.net/3p5z6zov/

Use prop() instead of attr()使用 prop() 而不是 attr()

►If you're using jQuery 1.6 or later, use prop() instead of attr() ►如果您使用的是 jQuery 1.6 或更高版本,请使用 prop() 而不是 attr()

lastClicked.find('input[type=checbox]').prop("checked")

When you are using prop() , the value returned will be Boolean当您使用prop() 时,返回的值将是布尔值

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM