更改按钮功能jQuery
Change button functionality jQuery
我在树枝中有这个代码
{% if followsId == null %}
<div id="followUser" class="follow" data-userId="{{ profileUserData.id }}" data-currentUserId="{{ loggedUserData.id }}" data-action="follow">
Follow
</div>
{% else %}
<div id="unFollowUser" class="follow" data-followsId="{{ followsId }}">
Unfollow
</div>
{% endif %}
我只是想把内容和功能改为点击按钮,并用jQuery尝试了这个代码
$('#followUser').click(function () {
var userId = $(this).attr('data-userId'),
action = $(this).attr('data-action'),
currentUser = $(this).attr('data-currentUserId');
$.ajax({
method: 'POST',
url: "/sci-profile/profile/follow",
data: {
userId: currentUser,
profileUserId: userId,
action: action
},
success: function(response)
{
$('#followUser').attr('id', 'unFollowUser')
.text('Unfollow')
.fadeOut(50)
.delay(50)
.fadeIn(50);
}
});
});
有了页面源代码,我在按钮上看到了不同的ID和不同的文本,但当第二次点击时,我调用了第一个按钮,就像它从未改变过一样。有没有办法刷新那个元素的内存,或者我从一开始就做错了什么?
我认为您的代码应该喜欢这个
{% if followsId == null %}
<div data-action="follow" class="follow" data-user-id="{{ profileUserData.id }}">
Follow
</div>
{% else %}
<div data-action="unfollow" class="follow" data-user-id="{{ followsId }}">
Unfollow
</div>
{% endif %}
无需存储当前登录的用户id,因为您可以从会话中获取:)
您的Ajax代码应该是这样的
$('.follow').click(function () {
var _this= $(this);
var userId = $(this).data('user-id');
var action =$(this).data('action');
$.ajax({
method: 'POST',
url: "/sci-profile/profile/follow",
data: {
profileUserId: userId,
action: action
},
success: function(response)
{
if(action=="follow")
{
_this.attr('data-action', 'unfollow').text('Unfollow');
}
else
{
_this.attr('data-action', 'follow').text('Follow');
}
}
});
});
希望你喜欢我的答案并投赞成票,如果正确,请标记为正确:)
相关文章:
- 重新启动游戏jQuery功能不工作
- 如何在iframe中激活jquery功能
- 为什么我不能使用jQuery功能
- 如何禁用缩略图的jquery功能
- 页面内容加载完毕后执行Jquery功能
- 按下空格键一次后,Jquery功能不起作用
- 单击滚动条时禁用Jquery功能
- 当屏幕大小小于480px时,如何禁用jQuery功能
- JQuery功能,可在任何地方增加字体大小
- 平移缩放 JQuery 功能
- jQuery功能,用于隐藏基于未在点击上运行的HTML5数据标签的元素
- 如何在循环中创建的多个按钮上使用相同的 jquery 功能
- 是否可以在更改单选按钮时禁用/启用jquery功能
- 如何使文本框值更改对 jQuery 功能生效
- 结合导航UL jQuery功能
- jQuery功能,根据粘贴文本时不起作用的字数限制文本输入
- mvc 4 jquery功能与dropdownlist和window.onload问题有关
- 固定导航条的JQuery功能不工作
- 单击导航选项卡时分配jQuery功能
- IE 11显示错误时,我点击jquery功能