javascript $('input[type=checkbox]').change(function()
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11233985/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
$('input[type=checkbox]').change(function()
提问by gilly3
Currently Im have the following script which checks to see if a checkbox value has changed but its not working when I try to use it!
目前我有以下脚本,用于检查复选框值是否已更改,但当我尝试使用它时它不起作用!
<script>
$('input[type=checkbox]').change(function () {
if ($(this).is(':checked')) {
if ($(this).prev().attr('checked') && $(this).val() != $(this).prev().val()) {
alert("previous checkbox has same value");
}
}
});?
</script>
<input name="" type="checkbox" value="here"/>(if this was checked)
<input name="" type="checkbox" value="here"/>(then this)
<input name="" type="checkbox" value="there"/>(would not allow prompt alert)
<input name="" type="checkbox" value="here"/>(would allow)?
you can see it working here yet it does not work when i try to use it http://jsfiddle.net/rajaadil/LgxPn/7
你可以看到它在这里工作,但当我尝试使用它时它不起作用 http://jsfiddle.net/rajaadil/LgxPn/7
The idea is to alert when a checked checkbox value is different from the previously checked checkbox value!
这个想法是在选中的复选框值与之前选中的复选框值不同时发出警报!
Currently my checkbox look like
目前我的复选框看起来像
<input type="checkbox" name="checkbox[]" onClick="getVal();setChecks(this)" value="`key`=<?php echo $rspatient['key']?>" class="chk" id="chk<?php echo $a++?>"/>
I thought the function ('input[type=checkbox]').change(function()
would get these but im wrong somewhere?
我以为函数('input[type=checkbox]').change(function()
会得到这些,但我错在哪里?
回答by gilly3
To select the previous checked sibling checkbox, use this:
要选择上一个选中的兄弟复选框,请使用以下命令:
$(this).prevAll(":checked:first")
I expected to use :last
, but :first
is what works. This is counter-intuitive to me and inconsistent with how :first
and :last
usually work, but I tested it in several browsers and the result is consistent.
我希望使用:last
,但这:first
是有效的。这对我来说是违反直觉的,并且:first
与:last
通常的工作方式和工作方式不一致,但我在多个浏览器中对其进行了测试,结果是一致的。
$(':checkbox').change(function() {
if (this.checked) {
var lastChecked = $(this).prevAll(":checked:first");
if (this.value == lastChecked.val()) {
alert("previous checked box has same value");
}
}
});
Demo:http://jsfiddle.net/LgxPn/15/
演示:http : //jsfiddle.net/LgxPn/15/
Edit:If by "previously checked checkbox" you mean the last box the user clicked, then you'll need to keep track of that yourself. There's no built-in jQuery method that will tell you anything about click history.
编辑:如果“先前选中的复选框”是指用户单击的最后一个框,那么您需要自己跟踪。没有内置的 jQuery 方法可以告诉您有关点击历史记录的任何信息。
What happens when the user first checks several boxes, and then checks and immediately unchecks a box? Should the next most recently checked box be used? If so, then you need to keep track of all checked boxes, and what order they were clicked. Here's how you can keep track of the checked boxes:
当用户首先选中几个框,然后选中并立即取消选中一个框时会发生什么?应该使用下一个最近选中的框吗?如果是这样,那么您需要跟踪所有选中的框,以及它们被点击的顺序。以下是跟踪选中框的方法:
var lastChecked = [];
$(':checkbox').change(function() {
if (this.checked) {
if (lastChecked.length && this.value == lastChecked[0].value) {
alert("the last box you checked has the same value");
}
lastChecked.unshift(this);
}
else {
lastChecked.splice(lastChecked.indexOf(this), 1);
}
});?
回答by Mike Robinson
Just a guess (with some better syntax), try this:
只是一个猜测(有一些更好的语法),试试这个:
<script type="text/javascript">
$(function() {
$('input:checkbox').change(function() {
if($(this).is(':checked'))
{
if ($(this).prev().prop('checked') && $(this).val() != $(this).prev().val()) {
alert("previous checkbox has same value");
}
}
});?
});
</script>
回答by Hymanwanders
Your alert message and your if
statement don't match. You check to see if the values !=
, but the alert says they are equal. I'm assuming the alert is the case you want to check for. Try:
您的警报消息和您的if
声明不匹配。您检查值!=
是否相等,但警报说它们相等。我假设警报是您要检查的情况。尝试:
$(':checkbox').change(function() {
var $this = $(this);
var $prev = $this.prev();
if($this.is(':checked')) {
if($prev.is(':checked') && $this.val() === $prev.val()) {
alert('Previous checkbox has same value');
}
}
});?
And as the others mentioned, make sure this is all within a $(document).ready()
block
正如其他人提到的,确保这一切都在一个$(document).ready()
块内
回答by C???
This seems to work for me in Chrome:
这在 Chrome 中似乎对我有用:
$(function() {
$('input[type=checkbox]').click(function() {
if ($(this).is(':checked')) {
if ($(this).prev().prop('checked')) {
if ($(this).val() != $(this).prev('input[type=checkbox]').val()) {
alert("previous checkbox has same value");
}
}
}
});
});?
Here's a JSFiddle: http://jsfiddle.net/gy8EQ/
这是一个 JSFiddle:http: //jsfiddle.net/gy8EQ/