即
$("#savenew").live('click', function(e) {
var user=<?php echo $user?>;
$.ajax({
type: "POST",
url: "actions/sub.php",
data:{user: user} ,
success: function(){
$('#savenew').html('<span>Unsubscribe</span>');
$(this).attr('id', 'clean'); // this my problem my problem
}
});
});
ajax 请求后的 id 没有从 savenew 更改为 clean,但 html 完全没问题,所以我知道 ajax 请求正在工作。你认为问题出在哪里?
最佳答案
您只需要保存上下文
(通过context
option for $.ajax()
),因此this
仍然引用#savenew
,如下所示:
$("#savenew").live('click', function(e) {
var user=<?php echo $user?>;
$.ajax({
context: this, //add this!
type: "POST",
url: "actions/sub.php",
data:{user: user} ,
success: function(){
$(this).attr('id', 'clean').html('<span>Unsubscribe</span>');
}
});
});
另请注意,这允许您稍微链接和清理事物。
关于javascript - jquery中元素的id不会改变?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4561729/