2016-11-25 230 views
0

我是jQuery的新的,我不知道如何使用它,这是我的代码创建样子如何使用jquery复选框?

<div id="checkbox-container"> 
    <div class="hidefull"> 
     <input type="checkbox" value="hideFull" id="check-hide-full-table">Hide Full 
    </div> 
    <div class="hideempty"> 
     <input type="checkbox" value="hideEmpty" id="check-hide-empty-table">Hide Empty 
     </div> 
</div> 

和jQuery我创建这样

function test(){ 
    alert("test called"); 
    $("#check-hide-empty-table").prop("checked", true); 
} 

所以问题是,怎样检查使用jQuery的复选框?我已经看this但仍未检查。有人知道我的代码有什么问题吗?

我想创建如果加载新页面,该复选框,选中。

仅供参考,警报显示。

+4

您有一个额外的','你'prop(“选中”,true)'。删除它,这应该工作正常。 – Santi

+0

啊sry,我的代码没有额外的','但仍然没有working.i只是错字,我已经编辑我的问题, – Pentolan

+0

当你打电话给你的'测试'功能? – jkris

回答

-1

这里是解决方案:Demo

$(document).ready(function() { 
    $('input').on('change', function() { 
     if ($(this).attr("checked")) { 
      $(this).attr("checked", false); 
     } else { 
      $(this).attr("checked", true); 
     } 
    }); 
}); 

当复选框被选中它,将它添加属性检查,如果不加以控制它拿出支票

0
jQuery(document).ready(function() { 

    test("#check-hide-empty-table"); 

}); 

function test(input){ 

    alert("test called"); 

    testInput = jQuery(input); 
    testInput.click(function(){ 
     if(this.checked != true) { 

      alert('not checked'); 
      return false; 
     } 

     alert('checked'); 
     return true; 

    }); 

}