与Remodal配对后,iCheck事件多次被触发

时间:2015-05-04 16:39:10

标签: javascript jquery icheck remodal

我目前正在使用:

  • remodalJS 0.6.4
  • iCheckJS 1.0.2

UX的设计使iCheck复选框和无线电充当过滤器并存在于重塑中。当我将两个库配对使用时,发生了奇怪的事情:

chrome devtool console screenshot

这是Chrome的控制台输出

  检查选择时(通过无线电或复选框),

ifChecked被触发一次。

<html>
    <head>
        <link rel="stylesheet" href="bower_components/remodal/dist/jquery.remodal.css">
        <link rel="stylesheet" href="bower_components/iCheck/skins/line/green.css">
    </head>
    <body>
        <h1 data-remodal-target="test-icheck-remodal"> Animal</h1>
        <h1 data-remodal-target="test-icheck-remodal-2"> Comics</h1>
        <div class="remodal" data-remodal-id="test-icheck-remodal">
            <h2>Choose Animal</h2>
            <input type="checkbox" value="">
            <label>Cat</label>
            <input type="checkbox" value="">
            <label>Dog</label>
        </div>

        <div class="remodal" data-remodal-id="test-icheck-remodal-2">
            <h2>Choose Comics</h2>
            <input type="checkbox" value="">
            <label>Batman</label>
            <input type="checkbox" value="">
            <label>Superman</label>
        </div>
        <script type="text/javascript" src="bower_components/jquery/dist/jquery.js"></script>
        <script type="text/javascript" src="bower_components/remodal/dist/jquery.remodal.js"></script>
        <script type="text/javascript" src="bower_components/iCheck/icheck.js"></script>
        <script type="text/javascript">
            $('input:checkbox, input:radio').each(function() {
                var self = $(this),
                label = self.next(),
                label_text = label.text();
                label.remove();
                self.iCheck({
                    checkboxClass: 'icheckbox_line-green',
                    radioClass: 'iradio_line-green',
                    insert: '<div class="icheck_line-icon"></div>' + label_text
                })
            });
            var flag = false;
            $(document).on('open', '.remodal', function(e) {
                var $this = $(this);
                var inputFields = $this.find('input:checkbox, input:radio');

                console.log('remodal Opened');
                if(!flag) {
                    inputFields.on('ifChecked', function(event) {
                        console.log('ifChecked called');
                    });

                    flag = true;
                }

            });
        </script>
    </body>
</html>

对应的bower.json

{
  "name": "remodal_icheck",
  "version": "0.0.0",
  "dependencies": {
    "remodal": "0.6.4",
    "iCheck": "1.0.2"
  },
  "devDependencies": {
  }
}

1 个答案:

答案 0 :(得分:1)

我认为这里的问题是你每次打开模态时都会继续绑定事件。我可以想到几个修复,但我不太确定它是否是处理它的最佳方法。

我的第一个解决方案: 创建一个标志,当您将一个标志为true的事件集绑定时,所以当它再次出现时,它不会再绑定它。

    // the flag
    var flag = false;
    $(document).on('open', '.remodal', function(e) {
        var $this = $(this);
        var elementId = $this.attr('data-remodal-id');
        var inputFields = $this.find('input:checkbox, input:radio');
        var queryParam = $this.attr('data-query-param');

        console.log('remodal Opened');

        // check flag if false
        if(!flag) {
          /* If it's radio, Close the remodal after selecting options */
          inputFields.on('ifChecked', function(event) {
            queryObject[queryParam] = 'hihi';       
            console.log('ifChecked called');
            if ($this.has('input:checkbox').length == 0) {
                $this.remodal().close();
            }
          });

          /* Todo: move to function clearFilters() */
          $this.find('.remodal-filter-clear').on('click', function() {
            inputFields.iCheck('uncheck');
          });

          flag = true;
        }

    });

<强>更新

根据您的评论添加第二个解决方案,有两个.remodal类。

我们为那些已经绑定到该事件的输入元素添加一个类。

对于此示例,我们将该类名称称为bound

    $(document).on('open', '.remodal', function(e) {
        var $this = $(this);
        var elementId = $this.attr('data-remodal-id');
        // find input elements which don't have the `.bound` class, then add class
        var inputFields = $this.find('input:checkbox:not(.bound), input:radio:not(.bound)').addClass("bound");
        var queryParam = $this.attr('data-query-param');

        console.log('remodal Opened');


          /* If it's radio, Close the remodal after selecting options */
          if(inputFields.length > 0) {
            inputFields.on('ifChecked', function(event) {
              queryObject[queryParam] = 'hihi';       
              console.log('ifChecked called');
              if ($this.has('input:checkbox').length == 0) {
                $this.remodal().close();
              }
            });

            /* Todo: move to function clearFilters() */
            $this.find('.remodal-filter-clear').on('click', function() {
              inputFields.iCheck('uncheck');
            });

          }
    });