Javascript jquery on()函数并使用遍历获取值/文本
如何使用Javascript jquery on()函数并使用遍历获取值/文本,javascript,jquery,ajax,jquery-traversing,Javascript,Jquery,Ajax,Jquery Traversing,如何使用on()函数获取值/文本 来自ajax响应的html w/php echo "<div class='cartItem-buyingItem'>"; echo "<a href='#' class='buyingItem-closed styleCrl-gry1 stxt10'>X<span class='pid'>".$row["prdct_id"]."</span><br></a>";
on()
函数获取值/文本
来自ajax响应的html w/php
echo "<div class='cartItem-buyingItem'>";
echo "<a href='#' class='buyingItem-closed styleCrl-gry1 stxt10'>X<span class='pid'>".$row["prdct_id"]."</span><br></a>";
echo "<div class='buyingItem-img'>";
echo "<img src='../".$row['prdct_picture']."'>";
echo "</div>";
echo "<div class='buyingItem-details styleCrl-gry'>";
echo "<div class='buyingItem-desc styleCrl-gry2 stxt12'>";
echo substr($row['prdct_description'],0,130)."...";
echo "</div>";
echo "</div>";
echo "<div class='buyingItem-origPrcng'>";
echo "<div class='buyingItem-unitPrice byngItm-untPrc stxt16 styleCrl-green'>". number_format($row['prdct_newPrice'] ,2) ."</div>";
echo "<div class='buyingItem-disPrice stxt12 styleCrl-gry2'>". number_format($row['prdct_price'] ,2) ."</div>";
echo "</div>";
echo "<div class='buyingItem-qty'>";
echo "<select class='buyingItem-qty-total'>";
echo "<option value='".array_count_values($allcart_num)[$row['prdct_id']]."'>".array_count_values($allcart_num)[$row['prdct_id']]."</option>";
echo "<option value='4'>4</option>";
echo "<option value='6'>6</option>";
echo "<option value='8'>8</option>";
echo "</select>";
echo "</div>";
echo "<div class='buyingItem-subPrcng'>";
echo "<div class='buyingItem-ttlPrice stxt16 styleCrl-green'>". number_format( bcmul(array_count_values($allcart_num)[$row['prdct_id']] , $row['prdct_newPrice'] ,2) ,2) ."</div>";
echo "<div class='buyingItem-ttlDisPrice stxt12 styleCrl-gry2'>". number_format( bcmul(array_count_values($allcart_num)[$row['prdct_id']] , $row['prdct_price'] ,2) ,2) ."</div>";
echo "</div>";
echo "</div>";
如何在jquery上使用右遍历来获取其他父对象的子对象值
我需要获取select
。buyingItem unitPrice
的值,然后乘以以选择值。buyingItem qty select
您的代码的问题是find()
用于查找子元素,而.buyingItem unitPrice
元素是select
的父元素的子元素。要检索它,您可以使用closest()
的组合来获取公共父级,然后使用find()
。试试这个:
$(".cartItem-Load").on('change', '.buyingItem-qty select', function() {
var unitPrice = $(this).closest('.cartItem-buyingItem').find('.buyingItem-unitPrice').text();
var total = parseInt($(this).val(), 10) * parseFloat(unitPrice);
console.log(total);
})
代码的问题是
find()
用于查找子元素,而.buyingItem unitPrice
元素是select
的父元素的同级的子元素。要检索它,您可以使用closest()
的组合来获取公共父级,然后使用find()
。试试这个:
$(".cartItem-Load").on('change', '.buyingItem-qty select', function() {
var unitPrice = $(this).closest('.cartItem-buyingItem').find('.buyingItem-unitPrice').text();
var total = parseInt($(this).val(), 10) * parseFloat(unitPrice);
console.log(total);
})
获取unitPrice的值/文本是一项工作,但在乘以it后返回NaN,这为您更新了答案。现在它工作了:)我添加了parseFloat(unitPrice.replace(',','')作为逗号。但是我如何返回小数点后两位的值?//123.20:)来更新这个:var total=(parseInt($(this.val(),10)*parseFloat(unitPrice.replace)(',','').toFixed(2);先生,我如何用新的总数更新.buyingItem ttlPrice?$(这个).closest('.cartItem buyingItem').find('.buyingItem ttlPrice').innerHTML=total;从获取单价的值/文本开始工作,但在乘以它之后返回NaN为您更新了答案。现在工作:)我添加了parseFloat(unitPrice.replace(','','','')对于逗号。但如何返回小数点后两位的值?//123.20:)以更新此值:var total=(parseInt($(this.val(),10)*parseFloat(unitPrice.replace(',','')).toFixed(2);先生,我如何更新。用新的总价购买商品TTL价格$(this).closest('.cartItem buyingItem').find('.buyingItem ttlPrice').innerHTML=total;