javascript - checkbox : onoff using button - Stack Overflow

There is a CheckBox & Button, I want to click on Button to checked & unchecked the checkBox wit

There is a CheckBox & Button, I want to click on Button to checked & unchecked the checkBox without touching the checkBox. How to do that ???

<input type = "checkbox"  id = "check" />
<input type = "button"  id = "buttonId" />

<script type = "text/javascript">
   $(function() {
       $("#buttonId").click( function() { 
            if( $("#check").is_NOT_CHECKED) {
                 CHECKED_CheckBox;
                        }         else 
                     if($("#check").is_CHECKED) {
                           UnCHECKED_CheckBox;
                           }    
                    });
   </script>

There is a CheckBox & Button, I want to click on Button to checked & unchecked the checkBox without touching the checkBox. How to do that ???

<input type = "checkbox"  id = "check" />
<input type = "button"  id = "buttonId" />

<script type = "text/javascript">
   $(function() {
       $("#buttonId").click( function() { 
            if( $("#check").is_NOT_CHECKED) {
                 CHECKED_CheckBox;
                        }         else 
                     if($("#check").is_CHECKED) {
                           UnCHECKED_CheckBox;
                           }    
                    });
   </script>
Share asked Jul 27, 2011 at 10:58 user744587user744587 1
  • Similar to stackoverflow./questions/426258/… ? – anothershrubery Commented Jul 27, 2011 at 11:00
Add a ment  | 

3 Answers 3

Reset to default 6
$("#buttonId").click( function() {
    // or .attr() prior to jQuery 1.6
    $("#check").prop('checked', function(i, val) {
        return !val;
    });
});

Depending on the context, you could also make use of the fact, that clicking on the label of a checkbox toggles its status:

<input type="checkbox" id="check" name="check"/> 
<label for="check">Some text</label>

Works without JavaScript :)

How about

$("#buttonId").click( function() {
    var checkbox = $("#check")[0];

    checkbox.checked = !checkbox.checked; // invert its checked status

});

demo at http://jsfiddle/gaby/czRkv/

Say these are your checkbox and button:

<input id="checkbox_id" type="checkbox" /><br>
<input id="button_id" type="button" value="Change" />​

Then you can perform checkbox on/off using this code:

​$("#button_id").click(function() {
    // if the checkbox is not checked
    if (! $("#checkbox_id").is(":checked")) {
        $("#checkbox_id").prop("checked", true);
    } else { // if the checkbox is checked
        $("#checkbox_id").prop("checked", false);        
    }
});​

See this live demo: http://jsfiddle/kvRG4/

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744931243a4601753.html

相关推荐

  • javascript - checkbox : onoff using button - Stack Overflow

    There is a CheckBox & Button, I want to click on Button to checked & unchecked the checkBox wit

    2天前
    60

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信