我是 Jquery 新手。我正在尝试显示获取的数据 w.r.to clicked item。 现在,在下面的示例中,如何将 products[0] 和 products[1] 显示到点击项目的 span 元素。
例如 - 预期输出:
item1 1 2
item2 3 4
HTML:
<table>
<tr>
<td><a href="" myval="item1">item1</a>
<span class="qty1"></span>
<span class="qty2"></span>
</td>
</tr>
<tr>
<td><a href="" myval="item2">item2
<span class="qty1"></span>
<span class="qty2"></span> </td>
</tr>
<tr>
<td><a href="" myval="item3">item3
<span class="qty1"></span>
<span class="qty2"></span> </td>
</tr>
</table>
jQuery:
$('a').click(function(){
var node=$(this);
var item = node.attr('myval');
$.post('month_copy.php', {"bcode":item,"month":"February"}, function(data){
var products = data.split("|");
node.find(".qty1").html(products[0]);
node.find(".qty2").html(products[1]);
});
});
month_copy.php:
echo 1."|".2 //item1
echo 3."|".4; //item2
我知道 jQuery 有问题,请给我建议。提前致谢。
最佳答案
td
不是 a
的 child ,.find
对 child 有效。
$('a').click(function(){
var node=$(this);
var item = node.attr('myval');
$.post('month_copy.php', {"bcode":item,"month":"February"}, function(data){
var products = data.split("|");
node.closest('td').find('.qty1').html(products[0]);
node.closest('td').find('.qty2').html(products[0]);
});
});
关于php - 如何使用 jQuery 在特定的点击项上显示从 php 检索到的数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36202746/