微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

使用 jquery 添加事件侦听器以动态生成 href

如何解决使用 jquery 添加事件侦听器以动态生成 href

我有很多用 ph​​p 动态生成的 href,我需要点击事件监听器。

这不是什么大问题,但是当我尝试点击其中一个时,总是返回相同的数据。

我有这个:

$(".qrCode").click(function(e) {
      e.preventDefault();
      e.stopPropagation();
      e.stopImmediatePropagation();

      $.ajax({
        url: "{{ route('restaurants.qrcodes',$id) }}",type: "GET",success:function(response){
          console.log(response);
        },error: function(jqXHR,textStatus,errorThrown){
          if (jqXHR.status === 0) {

            alert('Not connect: Verify Network.');

          } else if (jqXHR.status == 404) {

            alert('Requested page not found [404]');

          } else if (jqXHR.status == 500) {

            alert('Internal Server Error [500].');

          } else if (textStatus === 'parsererror') {

            alert('Requested JSON parse Failed.');

          } else if (textStatus === 'timeout') {

            alert('Time out error.');

          } else if (textStatus === 'abort') {

            alert('Ajax request aborted.');

          } else {

            alert('Uncaught Error: ' + jqXHR.responseText);

          }
        }
      });
  });

我的 html 是:

<a data-toggle="tooltip" data-placement="bottom" title="{{trans('lang.restaurant_qrcodes')}}" href="{{ route('restaurants.qrcodes',$id) }}" target="_blank" class='btn btn-link qrCode'>
      <i class="fa fa-qrcode"></i>
    </a>

更新

enter image description here

但我需要如果我想点击 href 女巫 id 12 转到 id 12,现在总是转到 id 11。我想我可以做一个 each 但我不知道我是怎么做的可以做到。

我需要这个,为了做,一个加载器 gif,同时进行网络操作

感谢帮助

解决方法

使用方法 unbind() 我知道我只能选择一个我想要的 href

解决方案:

$(".qrCode").unbind().click(function(event) {
    event.preventDefault();
    console.log($(this).attr('href'));
  }); 

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。