Javascript 将jquery响应分配给最近tr中类名的div
我想在选择defect\u type的对应行中显示类'defects'中jquery的响应。jquery请求工作正常,收到响应,但既不能在td部分中显示,也不能在t中的div中显示。我在成功部分写的代码行不起作用Javascript 将jquery响应分配给最近tr中类名的div,javascript,jquery,html,Javascript,Jquery,Html,我想在选择defect\u type的对应行中显示类'defects'中jquery的响应。jquery请求工作正常,收到响应,但既不能在td部分中显示,也不能在t中的div中显示。我在成功部分写的代码行不起作用 <html> <body> <table> <tr> <td> <select class="for
<html>
<body>
<table>
<tr>
<td>
<select class="form-control defect_type" onChange="get_defects(this.value)">
<option>1</option>
<option>2</option>
</select>
</td>
<td class="defects"></td>
</tr>
<tr>
<td>
<select class="form-control defect_type" onChange="get_defects(this.value)">
<option>1</option>
<option>2</option>
</select>
</td>
<td class="defects"></td>
</tr>
</body>
<script>
$(".defect_type").change(function(){
if(this.value==""){
alert("Select Defect Type");
return;
}
$.ajax({
type: 'post',
url: '../ajax/get_defects.php',
data: {
defect_type: this.value
},
success: function (response) {
$(this).closest('tr').find('.defects').html(response);
$(this).closest('tr').find('.defects').text(response);
}
});
});
</script>
</html>
1.
2.
1.
2.
$(“.defect_type”).change(函数(){
if(this.value==“”){
警报(“选择缺陷类型”);
返回;
}
$.ajax({
键入:“post”,
url:“../ajax/get_defects.php”,
数据:{
缺陷类型:this.value
},
成功:功能(响应){
$(this).closest('tr').find('.defects').html(响应);
$(this).closest('tr').find('.defects').text(响应);
}
});
});
问题:success函数中的$(this)不引用已更改的元素。它指的是AJAX
解决方案:将$(this)分配给变量,然后改用该变量
$(".defect_type").change(function(){
if(this.value=="") {
alert("Select Defect Type");
return;
}
var me = $(this); // <-- create the var
$.ajax({
type: 'post',
url: '../ajax/get_defects.php',
data: {
defect_type: this.value
},
success: function (response) {
me.closest('tr').find('.defects').html(response); // <--use the var here
me.closest('tr').find('.defects').text(response); // <--use the var here
}
});
});
$(“.defect\u type”).change(函数(){
if(this.value==“”){
警报(“选择缺陷类型”);
返回;
}
var me=$(this);//问题:success函数中的$(this)不是指更改的元素。它指的是AJAX
解决方案:将$(this)分配给变量,然后改用该变量
$(".defect_type").change(function(){
if(this.value=="") {
alert("Select Defect Type");
return;
}
var me = $(this); // <-- create the var
$.ajax({
type: 'post',
url: '../ajax/get_defects.php',
data: {
defect_type: this.value
},
success: function (response) {
me.closest('tr').find('.defects').html(response); // <--use the var here
me.closest('tr').find('.defects').text(response); // <--use the var here
}
});
});
$(“.defect\u type”).change(函数(){
if(this.value==“”){
警报(“选择缺陷类型”);
返回;
}
var me=$(this);//我怀疑这是$(this)
的范围问题。尝试在AJAX
调用之前将$(this)
赋值给变量,然后在success
子句中引用它。我怀疑这是$(this)
的范围问题。尝试赋值$(this)
调用之前的变量,然后在success
子句中引用它。