return 特定字段中的值如何具有相同 class jquery
How return value in specific field with same class jquery
如果您的字段具有相同的 class 但值不同,我知道有一种方法可以在 jquery 中使用 $(this) 发送值但是当返回函数结果时我想更新已单击的特定字段?我该怎么做 $(this) 正在用相同的 class.
更改每个字段
类是在循环中自动生成的,我不想为每个字段使用不同的 classes。
必须有一种方法可以实现这一点
这是我的代码。
$(".voteclick").click(function () {
var dynname = $(this).attr('name');
var votevalue = $(this).next().val();
$.ajax({
url:'http://localhost/wpcomnt/wp-content/themes/smart-mag-child/subrating.php',
type:"POST",
data:"dynname=" + dynname + "&" + dynname + "=" + votevalue + "&measure=" + $('#measure').val(),
dataType:'json',
success:function (mydata) {
if(votevalue == 'up') {
$(this).val('Agree: ' + mydata); // this must not change every .voteclick field it should change only the clicked one ?
}
else if(votevalue == 'down') {
$(this).val('Disagree: ' + mydata); // this must not change every .voteclick field it should change only the clicked one ?
}
//$(".voteclick").val('Agree: '+mydata);
},
error:function () {
$(".errormainbtn").slideDown();
}
});
});
请阅读此行旁边的评论$(this).val('Disagree: '+mydata);
有很多字段一旦我点击它们就会改变,因为 class 是相同的,并且自动生成请告诉我一种方法,通过它我只能改变我点击 class 的字段是
".votelick"
正如 Wolff 提到的,您需要在 ajax 调用的选项中设置 context:this
。默认情况下,在 success 函数中,$(this)
引用 jqXHR 对象。如果您的 ajax 调用在事件处理程序中,并且您设置了 context:this
,则成功函数中的 $(this)
将改为引用触发处理程序的元素。
这里是 working example
$(".voteclick").click(function () {
var dynname = $(this).attr('name');
var votevalue ='up';//changed for example
$.ajax({
url:'test.php',//changed for example
type:"POST",
context:this, //this is the line you need to add
data:"dynname=" + dynname + "&" + dynname + "=" + votevalue + "&measure=" + $('#measure').val(),
dataType:'text', //changed from 'json' for my example
success:function (mydata) {
if(votevalue == 'up') {
$(this).val('Agree: ' + mydata);
}
else if(votevalue == 'down') {
$(this).val('Disagree: ' + mydata);
}
},
error:function () {
$(".errormainbtn").slideDown();
}
});
});
如果您的字段具有相同的 class 但值不同,我知道有一种方法可以在 jquery 中使用 $(this) 发送值但是当返回函数结果时我想更新已单击的特定字段?我该怎么做 $(this) 正在用相同的 class.
更改每个字段类是在循环中自动生成的,我不想为每个字段使用不同的 classes。
必须有一种方法可以实现这一点
这是我的代码。
$(".voteclick").click(function () {
var dynname = $(this).attr('name');
var votevalue = $(this).next().val();
$.ajax({
url:'http://localhost/wpcomnt/wp-content/themes/smart-mag-child/subrating.php',
type:"POST",
data:"dynname=" + dynname + "&" + dynname + "=" + votevalue + "&measure=" + $('#measure').val(),
dataType:'json',
success:function (mydata) {
if(votevalue == 'up') {
$(this).val('Agree: ' + mydata); // this must not change every .voteclick field it should change only the clicked one ?
}
else if(votevalue == 'down') {
$(this).val('Disagree: ' + mydata); // this must not change every .voteclick field it should change only the clicked one ?
}
//$(".voteclick").val('Agree: '+mydata);
},
error:function () {
$(".errormainbtn").slideDown();
}
});
});
请阅读此行旁边的评论$(this).val('Disagree: '+mydata);
有很多字段一旦我点击它们就会改变,因为 class 是相同的,并且自动生成请告诉我一种方法,通过它我只能改变我点击 class 的字段是
".votelick"
正如 Wolff 提到的,您需要在 ajax 调用的选项中设置 context:this
。默认情况下,在 success 函数中,$(this)
引用 jqXHR 对象。如果您的 ajax 调用在事件处理程序中,并且您设置了 context:this
,则成功函数中的 $(this)
将改为引用触发处理程序的元素。
这里是 working example
$(".voteclick").click(function () {
var dynname = $(this).attr('name');
var votevalue ='up';//changed for example
$.ajax({
url:'test.php',//changed for example
type:"POST",
context:this, //this is the line you need to add
data:"dynname=" + dynname + "&" + dynname + "=" + votevalue + "&measure=" + $('#measure').val(),
dataType:'text', //changed from 'json' for my example
success:function (mydata) {
if(votevalue == 'up') {
$(this).val('Agree: ' + mydata);
}
else if(votevalue == 'down') {
$(this).val('Disagree: ' + mydata);
}
},
error:function () {
$(".errormainbtn").slideDown();
}
});
});