Jquery, How get checkbox unchecked event and checkbox value?

假如想象 提交于 2019-12-18 14:38:44

问题


I want to get checkbox value 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

$("#countries input:checkbox:not(:checked)").click(function(){
    var val = $(this).val();
    alert('uncheckd' + val);
}); 

Is it possible to get unchecked value in this way?


回答1:


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());
}); 

Working Demo




回答2:


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



回答3:


 $("#countries input:checkbox").on('change',function()
 {
   if(!$(this).is(':checked'))
      alert('uncheckd ' + $(this).val());
 }); 


来源:https://stackoverflow.com/questions/27265308/jquery-how-get-checkbox-unchecked-event-and-checkbox-value

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!