<div class="mt-repeater-item">
<div class="row mt-repeater-row">
<input type="text" name="product[0][price]" value="2" oninput="update_total(this)">
</div>
</div>
<div class="mt-repeater-item">
<div class="row mt-repeater-row">
<input type="text" name="product[1][price]" value="3" oninput="update_total(this)">
</div>
</div>
我的剧本
function update_total(input) {
var sum = 0;
$('.mt-repeater-item').each(function(index) {
var product_price = $("input[name=product["+index+"][price]]").val() ? $("input[name=product["+index+"][price]]").val() : 0;
sum = sum + product_price;
});
alert(sum);
}
Error Uncaught Error: Syntax error, unrecognized expression
=&gt;如何解决?
答案 0 :(得分:1)
function update_total(input) {
var sum = 0;
$('.mt-repeater-item').each(function(index) {
var product_price = $("input[name='product["+index+"][price]']").val() ? $("input[name='product["+index+"][price]']").val() : 0;
sum = sum + parseInt(product_price);
});
alert(sum);
}
将name
的值括在单引号
答案 1 :(得分:1)
您缺少名称引用 -
function update_total(input) {
var sum = 0;
$('.mt-repeater-item').each(function(index) {
var product_price = $("input[name='product["+index+"][price]']").val() ? $("input[name='product["+index+"][price]']").val() : 0;
sum = sum + product_price;
});
alert(sum);
}