jQuery .trigger('click')不会在$ .each中被触发

时间:2017-06-23 14:08:32

标签: javascript jquery

我有几个按钮,我想点击页面加载:

任何提示我如何做到这一点?

我试过

$(function() {
  /* ------- Init SSV Charts ------- */
  var $ssvBtns = $('.loadSSV');
  if ($ssvBtns.length) {
    $ssvBtns.each(function() {
      $(this).trigger('click'); //DOES NOT FIRE;
      $(this).html('CLICKED'); //DOES FIRE;
     // console.log($(this)); //GIVES THE 'a' OBJECT
    });
  }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<a href="#" class="btn btn-success rounded-0 loadSSV" data-id="1"><i class="fa fa-refresh"></i> Test1</a>
<a href="#" class="btn btn-success rounded-0 loadSSV" data-id="2"><i class="fa fa-refresh"></i> Test2</a>
<a href="#" class="btn btn-success rounded-0 loadSSV" data-id="3"><i class="fa fa-refresh"></i> Test3</a>

Click事件:

$(document).on('click', '.loadSSV', function (e) {
    e.preventDefault();
    console.log('clicked'); //NOT FIRED!
    var $btn = $(this);
    var $id = $btn.attr('data-id');
    if ($btn.hasClass('is-disabled')) {
        return false;
    }
    $btn.children('i').removeClass().addClass('fa').addClass('fa-spin').addClass('fa-spinner');
    $btn.addClass('is-disabled');

    ....
});

所以它甚至没有出现,因为console.log('clicked');永远不会触发

0 个答案:

没有答案