Javascript Jquery,如何获取复选框未选中事件和复选框值?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27265308/
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
Jquery, How get checkbox unchecked event and checkbox value?
提问by Miuranga
I want to get checkboxvalue using jQuery when to uncheck the checked checkbox and show that unchecked value in the popup. I've tried below code but it not work
我想checkbox在取消选中选中的复选框并在弹出窗口中显示未选中的值时使用 jQuery获取值。我试过下面的代码,但它不起作用
$("#countries input:checkbox:not(:checked)").click(function(){
var val = $(this).val();
alert('uncheckd' + val);
});
Is it possible to get unchecked value in this way?
是否有可能以这种方式获得未经检查的值?
回答by Milind Anantwar
Your selector will only attach event to element which are selected in the beginning. You need to determine check unchecked state when value is changed:
您的选择器只会将事件附加到在开始时选择的元素。您需要在值更改时确定检查未选中状态:
$("#countries input:checkbox").change(function() {
var ischecked= $(this).is(':checked');
if(!ischecked)
alert('uncheckd ' + $(this).val());
});
回答by Senthil
You should check the condition on click or change. I hope that my example will help you.
您应该检查单击或更改时的条件。我希望我的例子对你有帮助。
$("input:checkbox.country").click(function() {
if(!$(this).is(":checked"))
alert('you are unchecked ' + $(this).val());
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<input class="country" type="checkbox" name="country1" value="India" /> India </br>
<input class="country" type="checkbox" name="country1" value="Russia" /> Russia <br>
<input class="country" type="checkbox" name="country1" value="USA" /> USA <br>
<input class="country" type="checkbox" name="country1" value="UK" /> UK
回答by Prateek
$("#countries input:checkbox").on('change',function()
{
if(!$(this).is(':checked'))
alert('uncheckd ' + $(this).val());
});

