Javascript 如何更新$get回调jquery的id属性

Javascript 如何更新$get回调jquery的id属性,javascript,php,jquery,html,get,Javascript,Php,Jquery,Html,Get,我试图通过jquery更新按钮单击事件的id属性,成功调用执行得很好,但id属性没有按照要求更新,或者我在成功回调中检查选择器类时出错。。($(this.attr(“id”,“add”+$id);) $(文档).ready(函数(){ $(文档).on('click','.mybtnclass',函数(onclick,e){ $id=$(this.attr('id'); $(“#flash”).show(); $(“#flash”).fadeIn(400); $.get('abc.php',{

我试图通过jquery更新按钮单击事件的id属性,成功调用执行得很好,但id属性没有按照要求更新,或者我在成功回调中检查选择器类时出错。。(
$(this.attr(“id”,“add”+$id);


$(文档).ready(函数(){
$(文档).on('click','.mybtnclass',函数(onclick,e){
$id=$(this.attr('id');
$(“#flash”).show();
$(“#flash”).fadeIn(400);
$.get('abc.php',{id:$id},函数(msg){
数组=msg.split('/');
if(msg==0)//t检查值是否已删除
{
$('#'+$id).html('Add');
$('#'+$id).removeClass('btn-danger');
$('#'+$id).addClass('btn-success');
$(this.attr(“id”,“add”+$id);
$(“#flash”).hide();
}
else if(msg==1)//t检查是否添加了值
{
$('#'+$id).html('Remove');
$('#'+$id).removeClass('btn-success');
$('#'+$id).addClass('btn-danger');
$(this.attr(“id”,“delete”+$id);
$(“#flash”).hide();
}
});
});
});
HTML


abc.php

<?php 

    $getval=explode('_', $_GET['id']);
    $id=$getval['1'];
    $action = $getval['0'];

    if($action == 'delete')
    {
            //code for delete
            echo 'add/'.$id ;

    }
else
{
  // code to add ...
   echo 'delete/'.$id ;
}

?>

当您使用
$('#'+$id)
进行其他操作时。也可用于更改id

$('#'+$id).prop("id", "add_"+$id);
否则


我相信你的问题是关于“这个”


$(文档).ready(函数(){
$(文档).on('click','.mybtnclass',函数(onclick,e){
$id=$(this.attr('id');
var$this_obj=$(this);//将其保存到变量
$.get('abc.php',{id:$id},函数(msg){
//使用$this_obj代替$(this)
//因为现在你在另一个街区,“这”是不同的
});
});
});

不是根据所问的问题,而是一种工作方法

$(document).ready(function() {
            $('.mybtnclass').click(function(){
            $id=$(this).attr('name');
            $("#flash").show();
            $("#flash").fadeIn(400);
            $.getJSON('abc.php',{id:$id},function(data){
            alert(data.action + '-' + data.id); // to troubleshoot
            if( data.action == "add")
                    {
                        $('[name ='+ $id +']').html('Add ');
                        $('[name ='+ $id +']').removeClass('btn-danger');
                        $('[name ='+ $id +']').addClass('btn-success');
                        $("#flash").hide();
                        $('[name ='+ $id +']').attr('name', data.action+'_'+data.id);


                    }
            else if( data.action == "delete")
                    {
                        $('[name ='+ $id +']').html('Remove');
                        $('[name ='+ $id +']').removeClass('btn-success');
                        $('[name ='+ $id +']').addClass('btn-danger');
                        $("#flash").hide();
                        $('[name ='+ $id +']').attr('name', data.action+'_'+data.id);

                    }
            });
我将abc.php中的值作为

echo json_encode( array("action"=>"add","id"=>$id) );

你是如何测试身份证是否没有更新的?你甚至需要这个身份证吗?为什么要更改它?这就像添加和删除一个值。。单击时,每次都必须传递id才能更新到dbRight,但为什么要更改id?如果是同一个元素,就没有理由更改它的id。当然你没有在样式或事件删除中使用id。你的名字很简单,没有任何检查。谢谢@KevinB现在我完全移动到名称部分:)根据id,它仍然显示错误,现在将检查根原因。我将所有内容更改为name attr Thnaks作为第二个选项
var self = this; //Create another variable
$.get('abc.php',{id:$id},function(msg){
    $(self).attr("id", "add_"+$id); //Use other variable to perform operation
});
<script>
$(document).ready(function() {
    $(document).on('click', '.mybtnclass', function( onclick, e){
        $id=$(this).attr('id');
        var $this_obj = $(this); // save this to a variable
        $.get('abc.php',{id:$id},function(msg){
                // use $this_obj instead of $(this)
                // because now you are in another block, 'this' is different
        });

            });

        });
</script>
$(document).ready(function() {
            $('.mybtnclass').click(function(){
            $id=$(this).attr('name');
            $("#flash").show();
            $("#flash").fadeIn(400);
            $.getJSON('abc.php',{id:$id},function(data){
            alert(data.action + '-' + data.id); // to troubleshoot
            if( data.action == "add")
                    {
                        $('[name ='+ $id +']').html('Add ');
                        $('[name ='+ $id +']').removeClass('btn-danger');
                        $('[name ='+ $id +']').addClass('btn-success');
                        $("#flash").hide();
                        $('[name ='+ $id +']').attr('name', data.action+'_'+data.id);


                    }
            else if( data.action == "delete")
                    {
                        $('[name ='+ $id +']').html('Remove');
                        $('[name ='+ $id +']').removeClass('btn-success');
                        $('[name ='+ $id +']').addClass('btn-danger');
                        $("#flash").hide();
                        $('[name ='+ $id +']').attr('name', data.action+'_'+data.id);

                    }
            });
echo json_encode( array("action"=>"add","id"=>$id) );