【发布时间】:2018-08-03 03:00:53
【问题描述】:
我正在使用 Codeignator。我的 HTML 输出对我来说是正确的。
现在我正在做的是,用户将输入药物的名称,药丸的数量和数量,以便根据数量计算价格并显示它。公式为$single_price=$total_amount/$qty_number;
我在上面的图片中添加了medication name="SIPLA", no of pills=3 和amount=30。它将计算它并显示single price=10.00
到目前为止一切正常。
我们来谈谈“添加”按钮。如果任何用户想要多于一种药物,那么他/她应该点击“添加”按钮,它将显示与上述相同的字段。
我做了同样的过程,我添加了medication name="ZOCON 400", no of pills=4 和amount=60。它计算并显示了single price=20.00,这是错误的,它应该显示single price=15.00。
1) 为什么我得到单一价格=20.00,因为它没有提到在第一种药物中添加的药丸=3。所以它在谈论第一个数量。它应该不谈论药丸=4
2) 单一价格的计算也显示在两个字段中。第一个和第二个。我只需要第二个。
3) 如何在数据库中提交这些数据?
希望你能理解我的问题。
代码 查看
<div class="add_row">
<input type="text" name="medication_name[]" id="medication_name" class="form_control text_cap" placeholder="medication_name">
<span class="value_button" id="decrease" onclick="decreaseValue(1)" value="Decrease Value">-</span>
<input type="number" id="number1" class="qty_number form_control" name="qty_number[]" value="0" class="form_control"/>
<span class="value_button" id="increase" onclick="increaseValue(1)" value="Increase Value">+</span>
<input type="text" class="form_control" name="single_price[]" id="single_price" placeholder="single price" />
<input type="text" class="form_control" name="total_p_price[]" id="total_p_price" placeholder="total price" />
<div class="btn_row add_row_click"> <span> + </span> Add </div>
</div>
Ajax 和 Js
function increaseValue(n) {
var value = parseInt(document.getElementById('number' + n).value, 10);
value = isNaN(value) ? 0 : value;
value++;
document.getElementById('number' + n).value = value;
}
function decreaseValue(n) {
var value = parseInt(document.getElementById('number' + n).value, 10);
value = isNaN(value) ? 0 : value;
value < 1 ? value = 1 : '';
value--;
document.getElementById('number' + n).value = value;
}
$(document).ready(function() {
var max_fields = 20; //maximum input boxes allowed
var wrapper = $(".add_row"); //Fields wrapper
var add_button = $(".add_row_click"); //Add button ID
var x = 1; //initlal text box count
$(add_button).click(function(e){ //on add input button click
e.preventDefault();
if(x < max_fields){ //max input box allowed
x++; //text box increment
$(wrapper).append('<div class="custom_fields"><input type="text" name="medication_name[]" id="medication_name'+ x +'" class="form_control text_cap" placeholder="medication_name"><span class="value-button" id="decrease" onclick="decreaseValue('+ x +')" value="Decrease Value">-</span><input type="number" id="number'+ x +'" value="0" name="qty_member[]" /><span class="value-button" id="increase" onclick="increaseValue('+ x +')" value="Increase Value">+</span><br /><input type="text" class="form_control" name="single_price[]" id="single_price'+ x +'" placeholder="single price" /> <input type="text" class="form_control" name="total_p_price[]" id="total_p_price'+ x +'" placeholder="total price" /> <div class="btn_row remove_field"> <span> - </span> Remove </div></div>');
}
});
$(wrapper).on("click",".remove_field", function(e){ //user click on remove text
e.preventDefault();
//$(this).parent('custom_fields').remove();
$(this).closest('.custom_fields').remove();
x--;
})
$("body").on('keyup', 'input[id^=total_p_price]', function() {
//$('input[id^=single_price]').prop('disabled', true);
var total_p_price= $(this).val();
var qty_number = $('input[id^=number]').val();
$.ajax({
type : "POST",
url: baseUrl + "/Customer_control/calculate_total_p_price",
data: {total_p_price: total_p_price,qty_number:qty_number},
cache : false,
success: function(html) {
//alert(html);
$('input[id^=single_price]').val(html);
}
});
});
});
控制器
public function calculate_total_p_price(){
$total_p_price=$this->input->post('total_p_price');
$qty_number=$this->input->post('qty_number');
$single_price=$this->Customer_model->calculate_total_p_price($total_p_price,$qty_number);
echo $single_price;
}
型号
public function calculate_total_p_price($total_p_price,$qty_number){
// print_r($total_p_price);
if(empty($qty_number) || ($qty_number == 0)){
return 0;
}
elseif(empty($total_p_price) || ($total_p_price == 0)){
return 0;
}
elseif(!empty($total_p_price) && (!empty($qty_number) || ($qty_number>0))){
$single_price=$total_p_price/$qty_number;
return number_format((float)$single_price, 2, '.', '');
}
else{return 0;}
}
【问题讨论】:
-
添加按钮在您的表中动态插入一个新行,对吗?我认为问题在于计算使用了错误的 ID。
-
您能否显示在几次“添加”操作后生成的动态 html?您使用的“X”值可能有一个奇怪的增量。
-
@Eiji,是的,你是对的。HTML 已经添加到 AJAX 和 js 文件中
标签: javascript php jquery codeigniter-3