我是Jquery的新手。我正在尝试显示与单击的项目相关的提取数据。现在,在下面的示例中,如何将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
的子级,.find
适用于儿童。
$('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]);
});
});