我已经用Java代码编写了代码。但是我在附加的输入字段中发现了onclick事件,这是一个问题。谁能帮我为什么不起作用?
默认情况下,我有1个数量字段,并且在右侧有一个(+)添加按钮。单击添加按钮后,即会添加数量字段。
我下面还有总计字段。
所以问题是: 当我将值添加到默认数量字段时,onInput事件正在默认字段上工作。
但是在通过单击(+)添加按钮添加新的附加数量字段之后,当我在新的附加数量字段中输入任何值时,on Input事件将不起作用。
任何人都可以帮助我,为什么它不起作用?那我该怎么办?
//Javascript Code
function calculatePrice() {
var sum = 0;
var numOfInputs = $(".quantity").length;
$(".quantity").each(function (index,ele) {
if (!isnaN(this.value) && this.value.length != 0) {
sum += parseFloat(this.value);
}
});
$("#totalPrice").val(sum);
}
$(function () {
// compute the average at document ready
calculatePrice();
// compute the average whenever an input field change
$(".quantity").on('input',function() {
calculatePrice();
});
});
//HTML code
<div class="box-body field_wrapper">
<div class="form-group col-xs-2">
<label for="int">Quantity <?php echo form_error('quantity') ?></label>
<input type="text" class="form-control quantity" name="quantity[]" placeholder="Quantity" />
</div>
<div class="form-group col-xs-2 " style="margin-top: 35px;" >
<a href="javascript:void(0);" type="button" class="add_button btn-primary btn-sm" title="Add field"><i class="fa fa-plus"></i></a>
</div>
</div>
</div>
</div>