Javascript 复选框:使用按钮打开/关闭

Javascript 复选框:使用按钮打开/关闭,javascript,jquery,Javascript,Jquery,有一个复选框和按钮,我想点击按钮来选中和取消选中复选框而不触摸复选框。怎么做 <input type = "checkbox" id = "check" /> <input type = "button" id = "buttonId" /> <script type = "text/javascript"> $(function() { $("#buttonId").click( function() {

有一个复选框和按钮,我想点击按钮来选中和取消选中复选框而不触摸复选框。怎么做

<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>

$(函数(){
$(“#按钮ID”)。单击(函数(){
如果($(“#检查”)。未检查){
选中复选框;
}否则
如果($(“#检查”)。已检查){
未选中复选框;
}    
});
根据上下文,您还可以利用以下事实:单击复选框的
标签
,可切换其状态:

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

一些文本
不使用JavaScript也可以工作:)

怎么样

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

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

});

演示在

说这些是您的复选框和按钮:

<input id="checkbox_id" type="checkbox" /><br>
<input id="button_id" type="button" value="Change" />​
查看此实时演示:

类似于?Nice…uses.prop(propertyName,function(index,oldPropertyValue))覆盖添加到jqueryv1.6()
<input id="checkbox_id" type="checkbox" /><br>
<input id="button_id" type="button" value="Change" />​
​$("#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);        
    }
});​