jquery中元素的id不会改变?
即
$("#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请求正在工作。你认为问题是什么?
i.e.
$("#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
}
});
});
the id after the ajax request, is not changing from savenew to clean, but the html is perfectly fine, so i know the ajax request is working. what do u thiunk the problem is?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您只需要保存
context
(通过context
$.ajax() 的选项),因此this
仍然引用#savenew
,如下所示:另请注意,这允许你把事情链起来并清理一下。
You just need to save the
context
(via thecontext
option for$.ajax()
) sothis
still refers to#savenew
, like this:Also note that this allows you to chain and clean things up a bit.
success: function(){
中的$(this)
不引用$('#savenew')
。正如您在上面一行中所做的那样,您需要通过 id 引用它:
$(this)
insidesuccess: function(){
does not refer to$('#savenew')
.As you do in the line above, you need to reference it by id: