Jquery post未更改响应上的值
我有一些jquery在点击链接时运行(如下)。这允许将数据发送到mysql数据库,然后更新某个值。然而,这是行不通的。我添加了一个Jquery post未更改响应上的值,jquery,jquery-selectors,Jquery,Jquery Selectors,我有一些jquery在点击链接时运行(如下)。这允许将数据发送到mysql数据库,然后更新某个值。然而,这是行不通的。我添加了一个警报(响应),我得到了正确的结果,但是没有改变,这可能意味着我使用了错误的选择器 JQUERY: $('.upVote').click(function(e){ e.preventDefault(); answer_id = $(this).siblings('.record_id').attr('value'); $.p
警报(响应)
,我得到了正确的结果,但是
没有改变,这可能意味着我使用了错误的选择器
JQUERY:
$('.upVote').click(function(e){
e.preventDefault();
answer_id = $(this).siblings('.record_id').attr('value');
$.post('../answerrating.php' , {answer_id: answer_id} , function(response){
$(this).prev('.ratingBox').val(response);
});
});
HTML:
此
不会引用在AJAX回调中单击的元素。尝试存储事件处理程序的上下文,并在回调函数中使用它
此外,在修改非输入元素的内容时,应使用或:
$('.upVote').click(function(e){
var self = this;
e.preventDefault();
var answer_id = $(this).siblings('.record_id').attr('value');
$.post('../answerrating.php' , {answer_id: answer_id} , function(response){
$(self).prev('.ratingBox').html(response);
});
});
该错误可能是因为
.post()
s success回调中的特殊变量this
的值几乎肯定不是click事件处理程序响应的DOM元素
要解决此问题,请在单击回调中为this
的值设置一个变量,并在成功回调中引用它。一般来说,这是一种很好的做法,如果您不能100%确定此
的值,因为它会因上下文而发生显著变化
$('.upVote').click(function(e){
var button = this;
e.preventDefault();
answer_id = $(this).siblings('.record_id').attr('value');
$.post('../answerrating.php' , {answer_id: answer_id} , function(response){
$(button).prev('.ratingBox').val(response);
});
});
试试这个
$('.upVote').click(function(e){
e.preventDefault();
var target_elem = $(this).prev('.ratingBox');
answer_id = $(this).siblings('.record_id').attr('value');
$.post('../answerrating.php' , {answer_id: answer_id} , function(response){
target_elem.text(response);
});
});
什么是回应??html???@dku.rajkumar响应是一个类似于“25”的php响应数字。我想把这个号码放在里面。评级箱可能是重复的,请尝试我的答案。。。希望它能起作用。将答案id行更改为
var answer\u id=$(按钮).
也可能有帮助,确保使用var
关键字避免创建全局变量。@user802370:响应是什么样的?这是你所期待的吗?@user802370:Oops!刚刚注意到另一个问题——确保对p
标记使用.text()
或.html()
而不是val
。
$('.upVote').click(function(e){
e.preventDefault();
var target_elem = $(this).prev('.ratingBox');
answer_id = $(this).siblings('.record_id').attr('value');
$.post('../answerrating.php' , {answer_id: answer_id} , function(response){
target_elem.text(response);
});
});