【问题标题】:icheck checkbox not getting uncheckedicheck 复选框没有被取消选中
【发布时间】:2017-09-23 05:52:39
【问题描述】:

我有以下表单界面

在这里,我使用了 icheck 复选框。当我手动选中和取消选中复选框时,它工作正常,因为我编写了以下代码

//if checkbox "Product Feed Url" is unchecked,show the feed textbox
$('#crawlingUrl').on('ifUnchecked', function(event){
    $("#feedUrl").show();
});

//if checkbox of "Product Feed Url" is checked,hide the feed textbox
$('#crawlingUrl').on('ifChecked', function(event){
    $("#feedUrl").hide();
    $("#feedUrl-error").hide();
});

单击取消按钮时,我重置表单元素并将其隐藏并显示商店 div 列表。在取消按钮上,我也尝试使用以下选项取消选中复选框,但它们都不起作用。

//$("#crawlingUrl").iCheck('uncheck');
//$("#feedDiv").find('#crawlingUrl').iCheck('uncheck');
//$("#feedDiv .clsCheckBox").iCheck('uncheck');
$("input[name=crawlingUrl]").iCheck('uncheck');

当我再次打开表单输入值时,复选框并没有被取消选中。有人可以告诉我如何解决这个问题吗?

检查元素的屏幕截图

【问题讨论】:

  • 请编辑问题并将html和jquery代码粘贴为文本而不是图像,以便我们直接复制查看
  • 编辑了问题。
  • 请粘贴您的原始 HTML。根据您的确切情况创建一个小提琴

标签: jquery checkbox icheck


【解决方案1】:

不是这个

$("input[name=crawlingUrl]").iCheck('uncheck');

试试这个

$("input[name=crawlingUrl]").removeAttr('checked').iCheck('update');

【讨论】:

    【解决方案2】:

    我认为你可以混合使用form 和'iCheck checkbox',我的例子是可以的:

    $('input').iCheck({
      // base class added to customized checkboxes
      checkboxClass: 'custom-icheckbox'
    });
    //if checkbox "Product Feed Url" is unchecked,show the feed textbox
    $('#crawlingUrl').on('ifUnchecked', function(event){
        $("#feedUrl").show();
    });
    
    //if checkbox of "Product Feed Url" is checked,hide the feed textbox
    $('#crawlingUrl').on('ifChecked', function(event){
        $("#feedUrl").hide();
        $("#feedUrl-error").hide();
    });
    function resetForm(){
    $('form')[0].reset();
    //$("#crawlingUrl").iCheck('uncheck');
    //$("#feedDiv").find('#crawlingUrl').iCheck('uncheck');
    //$("#feedDiv .clsCheckBox").iCheck('uncheck');
    $("input[name=crawlingUrl]").iCheck('uncheck');
    }
    .custom-icheckbox {
      height: 20px;
      width: 20px;
      border: 1px solid red;
    }
    .resetBtn {
    border:1px solid gray;
    width: 50px;
    height: 30px;
    }
    <script
      src="https://code.jquery.com/jquery-2.2.4.min.js"
      integrity="sha256-BbhdlvQf/xTY9gja0Dq3HiwQF8LaCRTXxZKRutelT44="
      crossorigin="anonymous"></script>
      <script src="https://cdnjs.cloudflare.com/ajax/libs/iCheck/1.0.2/icheck.js"></script>
      <input type="checkbox" id="crawlingUrl" name="crawlingUrl" value="enable" />
      <form action="/">
    <label for="feedUrl">hint: </label>
    <input type="text" id="feedUrl" name="feedUrl"/>
    <label id="feedUrl-error" for="feedUrl">Error</label>
    <div class="resetBtn" onClick="resetForm()">reset</div>
    </form>

    【讨论】:

      【解决方案3】:

      还有另一种极端情况,即尝试在 iCheck 自己的 ifChecked / ifUnchecked 处理程序中更改同一复选框的值,即使是

      .removeAttr('checked').iCheck('update')
      

      方法似乎不起作用。在这种情况下,似乎需要一个计时器破解,如per here

      setTimeout(function() { $("#myCheckBox").iCheck('uncheck'); }, 1);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-01-11
        • 1970-01-01
        • 2015-04-30
        • 1970-01-01
        • 2014-08-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多