【问题标题】:Jquery if checkbox is checked Bootstrap switchJquery是否选中了复选框引导开关
【发布时间】:2014-08-08 07:58:47
【问题描述】:

我使用 Bootstrap 开关插件使我的复选框看起来像开关按钮。我需要检查复选框是否在 jQuery 中被选中。我用谷歌搜索了很多,我尝试了一些建议和代码 sn-ps,但没有成功。

我认为我找到的最干净的代码是

$('#checkbox').attr('checked');

但它仍然无法在我的网站上运行。我已经在 head 中声明了 jQuery。我尝试在非引导开关复选框上使用这个 sn-ps,但仍然没有成功。

JS:

 <script>
    $(function(){
      $(".odeslat").click(function(){
           $('#pozadavky').slideUp(100);
           $('#datumpick').slideDown(300);

       var typpaliva = 'nic';
       var malpg = 'nic';¨

       /***Important condition***/

       if ($('#uho').attr('checked')) {
            $.cookie("typpaliva", "Diesel");
       }
       else {
            $.cookie("typpaliva", "Benzin");
       }


 /**********************************************/


    $.ajax({
   url: 'http://podivej.se/script_cas.php',
   data: {druh: '30'},
   type: "POST",
   success: function(data){
            alert($.cookie('typpaliva')); 
            alert($.cookie('malpg'));   
   }
   });

  });
});
</script> 

HTML

<input type="checkbox" name="testcheckbox" id="uho"/>

【问题讨论】:

  • “它仍然无法在我的网站上运行” 请更准确。什么确切地不起作用?发生什么了?你期望会发生什么?你做了什么来调试问题?
  • 如何触发开关?

标签: javascript jquery twitter-bootstrap checkbox


【解决方案1】:

使用:

if ($('#uho').is(':checked')) {

而不是

if ($('#uho').attr('checked')) {

【讨论】:

  • @BhushanKawadkar 您如何判断用户何时选中或取消选中?
  • @pathros,您可以注册.click(function()...change(function().. 事件处理程序,然后检查$(this).is(':checked'),如果选中则返回true,如果未选中则返回false。
【解决方案2】:

Demo

使用 .prop() 代替 .attr()

  • .prop 返回 - “真”或“假”。 使用这个
  • .is(':checked') 返回 - 'true' 或 'false' -:checked 是伪选择器。 或者这个
  • .attr 返回 - 'checked' 或 'attribute undefined'。

$('#uho').prop('checked')

if($('#uho').prop('checked')){
    $.cookie("typpaliva", "Diesel");
}
else {
    $.cookie("typpaliva", "Benzin");
}

【讨论】:

    【解决方案3】:

    根据已接受答案中的评论,这是我为引导程序中的条款和条件复选框解决此问题的方法:

    HTML

    <div class="checkbox">
       <label>
          <input id="agreeTAC" type="checkbox" value="">
          I have read and agree to the TERMS AND CONDITIONS listed on this page.
       </label>
    </div>
    

    Javascript(使用 jQuery)

    $("#agreeTAC").change(function(){
        var agreed = $(this).is(':checked');
        console.log("agreeTAC value changed and is ", agreed);
        if(agreed === true) { 
           // do 'true' stuff, like enabling a 'Continue' button
        }
        else {
           // do 'false' stuff, like disabling a 'Continue' button
        }
    })
    

    【讨论】:

      【解决方案4】:

      $('#uho').attr('checked') 将返回未定义。您可以使用$('#uho:checkbox:checked').length 属性来检查复选框是否被选中。试试这个 sn-p:

       if ($('#uho:checkbox:checked').length > 0) {
              $.cookie("typpaliva", "Diesel");
         }
         else {
              $.cookie("typpaliva", "Benzin");
         }
      

      【讨论】:

        【解决方案5】:
        • 在页面加载时使用此代码

          document.getElementById("#uho").checked = true;

        【讨论】:

        • 请告诉为什么OPjQuery 时有必要这样做?
        • 这是直接法。您可以为此目的使用 java 脚本。
        【解决方案6】:

        使用

          if($('#chkBoxID').is(":checked")){ // do stuff }
        

        【讨论】:

          猜你喜欢
          • 2016-03-04
          • 2012-07-13
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2021-02-05
          • 2012-06-23
          • 2011-05-04
          • 1970-01-01
          相关资源
          最近更新 更多