【问题标题】:Javascript custom event fire from a function with returning falseJavascript自定义事件从返回false的函数触发
【发布时间】:2017-09-11 08:46:14
【问题描述】:

我有以下表单代码,我必须在触发自定义事件后阻止表单提交。我还在提交函数上编写了一个 Javascript 来触发该事件,然后返回 false。如果我做错了,请帮忙。

表单 html:

   <form class="form-inline" action="" name="filterform">
      <div class="row">
        <?php foreach($formatted_post_type as $filters){?>
        <div class="checkbox">
          <label class="checkbox-inline" style="line-height:40px;">
            <input type="checkbox" name="filters[posttype][]" value="<?php echo $filters['slug']?>" onclick="refresh_posttypes();" />
            <?php echo $filters['name']?></label>
        </div>
        <?php }?>
      </div>
      <div class="row">
        <div class="col-md-12">
          <input type="submit" class="btn btn-primary btn-sm" value="Apply">
        </div>
      </div>
    </form>

javascript 代码:

jQuery(document).ready(function(){
  jQuery('form[name="filterform"]').on('submit', function(e){
      jQuery(document).triggerHandler('favorites-update-all-lists', function(e){
           e.preventDefault();
    });
    return false;
    });
  });

【问题讨论】:

    标签: javascript html jquery triggers jquery-events


    【解决方案1】:

    尝试在提交处理程序之后移动e.preventDefault();

    jQuery(document).ready(function(){
      jQuery('form[name="filterform"]').on('submit', function(e){
          e.preventDefault();
    
          jQuery(document).triggerHandler('favorites-update-all-lists', function(e){
    
          });
        return false;
        });
      });
    

    【讨论】:

      【解决方案2】:

      试试这个

      $(function() {
      
          $('form[name="filterform"]').on('submit', function(e) {
              $(document).triggerHandler('favorites-update-all-lists', function() {
                  e.preventDefault();
              });
          });
      
      });
      

      你在两个匿名函数中都使用了 e

      【讨论】:

        猜你喜欢
        • 2015-12-27
        • 1970-01-01
        • 1970-01-01
        • 2019-12-13
        • 1970-01-01
        • 2015-04-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多