简体   繁体   English

更改ID后jQuery onClick函数不起作用

[英]jQuery onClick function didn't work after change of id

I'm creating follow button using AJAX in Laravel. 我在Laravel中使用AJAX创建关注按钮。 When I click follow, it will turn into unfollow with the change of id. 当我单击“跟随”时,它将变为取消跟随id的更改。 I tried to click unfollow button again, it didn't execute unfollow on click function but remain the previous follow on click function. 我尝试再次单击“取消关注”按钮,它没有在单击功能上执行取消关注,但保留了先前的单击功能。

HTML 的HTML

 <a id="followAnchor" type="submit" class="btn btn-info" /><i class="fa icon-user-follow"></i> Follow</a>
                                                                     </div>

AJAX AJAX

$('#followAnchor').on("click", function(e){
var followId = document.getElementById('followAnchor');
 $.ajax({
          url: "{{ URL::route('follow-post') }}",
          type: 'POST',
          data: JSON.stringify(arr),
          contentType: 'application/json; charset=utf-8',
          dataType: 'json',
    }).done(function(response) {

          if(response.errors)
          {

          }
          else if (response.success){


            followId.innerHTML = "<i class='fa icon-user-follow'></i>" + "Unfollow";
            document.getElementById('followAnchor').id = 'unfollowAnchor';
          }
    });
}

$('#unfollowAnchor').on("click", function(e){
    var unfollowId = document.getElementById('unfollowAnchor');

    $.ajax({
          url: "{{ URL::route('unfollow-post') }}",
          type: 'POST',
          data: JSON.stringify(arr),
          contentType: 'application/json; charset=utf-8',
          dataType: 'json',
    }).done(function(response) {

          if(response.errors)
          {

          }
          else if (response.success){
            unfollowId.innerHTML = "<i class='fa icon-user-follow'></i>" + "Follow";
            document.getElementById('unfollowAnchor').id = 'followAnchor';
          }
    });
});

Events do not magically get bound after they are changed. 更改事件后,它们不会魔术般地受到约束。 Once the code has run it does not keep checking for new elements. 一旦代码运行,它就不会继续检查新元素。

  • You either need to unbind/bind the event when you change the id/ add new elements. 更改ID或添加新元素时,您都需要取消绑定/绑定事件。
  • Do not change the id, just add logic into the original click (what most devs do) 不更改ID,只需在原始点击中添加逻辑即可(大多数开发人员会这样做)
  • Or use event bubbling. 或使用事件冒泡。

Bubbling example: 冒泡示例:

$(document).on("click", "#followAnchor", function(e){ /* code here */ });        
$(document).on("click", "#unfollowAnchor", function(e){ /* code here */ });

You need to use event-delegation , because you are register a event-listiner on an element that not exist by that time. 您需要使用event-delegation ,因为您要在当时不存在的元素上注册一个event-listiner。

$('#unfollowAnchor').on("click", function(e){

has to be 必须

$(document).on("click", '#unfollowAnchor',function(e){

Instead of using IDs here, I would go with adding or removing specific classes: 除了在这里使用ID之外,我还可以添加或删除特定的类:

$(followId).addClass('unfollow').removeClass('follow');

as an example. 举个例子。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM