[英]Updating data-attr on a button with jQuery
I'm trying to update the data-action
attribute for the ('.follow')
element to unfollow
in the success response but it's not updating. 我正在尝试更新
('.follow')
元素的data-action
属性,以在成功响应中unfollow
,但它不会更新。
What am I doing wrong? 我究竟做错了什么?
$('.follow').click(function() {
var id = $(this).data("id");
var action = $(this).data("action");
var url = '/action/' + action;
$.ajax({
url: url,
headers: {
'TOKEN': $('meta[name="csrf-token"]').attr('content')
},
data: {
id: id
},
type: "POST",
success: function(v) {
if (v.response == "OK") {
$(this).data('action', 'unfollow');
}
if (v.response == "E") {
alert('there was an error');
}
}
});
});
EDIT 编辑
Adding the fact that the element in question is a button, as the offered solutions below are still not working. 添加以下事实:所讨论的元素是一个按钮,因为下面提供的解决方案仍然无效。
<button class="follow" data-id="<?php echo $profile->id; ?>" data-action="follow">FOLLOW</button>
2nd EDIT 第二次编辑
Just want to update this question in case anybody else finds themselves in a similar situation. 只是想更新这个问题,以防其他人发现自己处于类似情况。
As it turns out, the accepted answer from Nir Tzezana was absolutely correct. 事实证明,Nir Tzezana接受的答案绝对正确。 The reason I couldn't get it to work was due to Firefox being buggy.
我无法让它工作的原因是由于Firefox有问题。 It's not the first time something like this has happened, but I always forget that this could be an issue.
这不是第一次发生这样的事情,但我总是忘记这可能是一个问题。
So, the moral of the story.. If you are using Firefox and you are confident that you have working code - yet it doesn't seem to be working - quit Firefox and reboot it (especially if you've had the browser open for a long time). 所以,这个故事的寓意......如果你使用的是Firefox并且你确信你有工作代码 - 但它似乎没有工作 - 退出Firefox并重新启动它(特别是如果你已经打开了浏览器很长时间)。 Chances are your code was working the whole time.
机会是你的代码一直在工作。
$(this)
refers to the return function. $(this)
指的是返回函数。
Use a fat arrow function or this self
trick. 使用胖箭头功能或这个
self
技巧。
$('.follow').click(function () {
var id = $(this).data("id");
var action = $(this).data("action");
// Define self to be the .follow element
var self = $(this);
var url = '/action/' + action;
$.ajax({
url: url,
headers: {
'TOKEN': $('meta[name="csrf-token"]').attr('content')
},
data: {
id: id
},
type: "POST",
success: function(v){
if (v.response == "OK") {
self.data('action', 'unfollow');
}
if (v.response == "E") {
alert('there was an error');
}
}
});
});
this
deosn't refers to current element ie .follow
in the $.ajax()
success handler. this
deos not引用当前元素,即$.ajax()
成功处理程序中的.follow
。 You can use $.ajax()
context option 您可以使用
$.ajax()
上下文选项
$('.follow').click(function () {
$.ajax({
context: this,
success: function(){
//this: it will refer to clicked button
}
});
});
And Default action of <button>
element is submit
, if the button is wrapped in a form use type="button"
并且
<button>
元素的默认操作是submit
,如果按钮被包装在表单中,则使用type="button"
<button type="button" class="follow" data-id="<?php echo $profile->id; ?>" data-action="follow">FOLLOW</button>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.