我使用表单中继器创建发票。发票有多个产品来自数据库和价格。 当我选择产品时,其自动价格出现在费用框中,并且还有其他输入框,如折扣和百分比,默认值为0 0。第一行工作得很好。但是当我点击添加更多按钮来生成下一行添加另一个产品。Php不工作添加更多行(中继器)
在下一行中,我可以从下拉列表中选择产品。但其费用没有出现,其他输入框值如折扣和百分比也消失(他们没有显示任何默认值。)
这是我的HTML代码。
<div class="row">
<div class="col-xs-3">
<label for="single" class="control-label">Select Invoice Type</label>
</div>
<div class="col-xs-2">Fee</div>
<div class="col-xs-1">Quantity</div>
<div class="col-xs-1">Discount</div>
<div class="col-xs-2">Total</div>
<div class="col-xs-1">Doctor %</div>
</div>
<div class="mt-repeater">
<div data-repeater-list="group-b">
<div data-repeater-item class="row">
<div class="col-xs-3">
<div class="form-group">
<select id="invoice" class="form-control" name="invoice">
<option></option>
<?php
$select = mysqli_query(connection(), "select * from treatement");
while($r = mysqli_fetch_array($select)) {
echo "<option value='$r[0]'>$r[name]</option>";
}
?>
</select>
</div>
</div>
<div class="col-xs-2">
<div class="form-group">
<input type="text" value="" class="form-control" placeholder="Fee" name="fees" id="fees">
</div>
</div>
<div class="col-xs-1">
<div class="form-group">
<input type="text" class="form-control" value="1" name="quantity" >
</div>
</div>
<div class="col-xs-1">
<div class="form-group">
<input type="text" value="0" class="form-control" placeholder="Discount" name="discount">
</div>
</div>
<div class="col-xs-2">
<div class="form-group">
<input type="text" value="0" class="form-control" placeholder="Total" readonly name="total">
</div>
</div>
<div class="col-xs-2">
<div class="form-group">
<input type="text" value="0" class="form-control" placeholder="%" name="percentage">
</div>
</div>
<div class="col-md-1">
<a href="javascript:;" data-repeater-delete class="btn btn-danger">
<i class="fa fa-close"></i>
</a>
</div>
</div>
</div>
<a href="javascript:;" data-repeater-create class="btn btn-info mt-repeater-add">
<i class="fa fa-plus"></i> Add More
</a>
<br>
<br>
</div>
而且我使用js函数来比较每个产品的费用。这有助于我在一个盒子里显示费用。
$(document).ready(function() {
$("#invoice").change(function() {
var value = $(this).val();
$.get("get_fees.php", {
id: value
}, function(data) {
$("#fees").val(data);
})
})
})
您是否在每一行使用'id =“invoice”'? ID必须是唯一的,您应该使用类而不是ID。然后请参阅[动态创建的元素上的事件绑定](https:// stackoverflow。com/questions/203198/event-binding-on-dynamic-created-elements),以便为它们绑定事件处理程序。 – Barmar
注意:'mysqli'的面向对象的接口明显不那么冗长,使得代码更易于阅读和审计,并且不容易与陈旧的'mysql_query'接口混淆。在你过于投入程序风格之前,它是值得转换的。例如:'$ db = new mysqli(...)'和'$ db-> prepare(“...”)过程接口是PHP4时代的一个神器,当引入mysqli API时,不应该在新的码。 – tadman