Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/79.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 使用jQuery更新按钮上的数据属性_Javascript_Jquery_Ajax_Jquery Selectors - Fatal编程技术网

Javascript 使用jQuery更新按钮上的数据属性

Javascript 使用jQuery更新按钮上的数据属性,javascript,jquery,ajax,jquery-selectors,Javascript,Jquery,Ajax,Jquery Selectors,我试图在成功响应中将('.follow')元素的数据操作属性更新为unfollow,但它没有更新 我做错了什么 $('.follow').click(function() { var id = $(this).data("id"); var action = $(this).data("action"); var url = '/action/' + action; $.ajax({ url: url, headers: {

我试图在成功响应中将
('.follow')
元素的
数据操作
属性更新为
unfollow
,但它没有更新

我做错了什么

$('.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');
            }
        }
    });
});
编辑

添加一个事实,即所讨论的元素是一个按钮,因为下面提供的解决方案仍然不起作用

<button class="follow" data-id="<?php echo $profile->id; ?>" data-action="follow">FOLLOW</button>
$(此)
指返回函数。
使用胖箭头功能或此
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');
           }
       }
   });

});

deosn不引用当前元素,即
。请遵循
$.ajax()成功处理程序中的
。你可以用

元素的默认操作是
submit
,如果按钮包装在表单中,请使用
type=“button”


但它没有更新--您是如何确保它不工作的?1)因为它没有更新元素,2)因为当我再次单击按钮时,它向服务器发送的是follow操作,而不是unfollow操作。这两种解决方案都没有工作,尽管我可以看到它们都是正确的。('.follow')元素是一个按钮这一事实有什么不同吗<代码>尝试
self.attr(“数据操作”,“取消跟踪”)
;该死的。自我数据。。。毕竟他在工作。Firefox只是在玩游戏。这已经不是我第一次因为Firefox需要重启而浪费几个小时了。感谢大家的帮助。以前从未使用过
context
。这太棒了!虽然在这种情况下它不起作用。也许是因为我调用了一个按钮元素<代码>@spice,你的纽扣包在表格里了吗?然后使用
type=“button”
@Saptal尝试了这一点,并尝试将按钮更改为常规div.

$('.follow').click(function () {
    $.ajax({
        context: this,
        success: function(){
           //this: it will refer to clicked button
        }
    });
});
<button type="button" class="follow" data-id="<?php echo $profile->id; ?>" data-action="follow">FOLLOW</button>