动态输入字段值不计算具有正确值的值

我正在使用Codeignator。 我的HTML输出对我来说是正确的。

在此处输入图像描述 现在我正在做的是,用户将输入药物的名称,没有药丸和金额,以便根据金额计算价格并显示它。 公式为$single_price=$total_amount/$qty_number;

在此处输入图像描述

上面的图像我添加了medication name="SIPLA", no of pills=3amount=30 。 它将计算它并显示single price=10.00

现在一切都很完美。

我们来谈谈“ADD”按钮。 如果任何用户想要多于一种药物,那么他/她应该点击“添加”按钮,它将显示与上面相同的字段。

我做了同样的过程,我添加了medication name="ZOCON 400", no of pills=4amount=60 。 它计算并显示single price=20.00这是错误的,应该显示single price=15.00

1)为什么我得到单一价格= 20.00,因为它没有说药丸= 3,这是在第一种药物中添加的。 所以它正在谈论第一个数量。 它应该说没有药丸= 4

2)单个价格的计算也显示在两个字段中。 第一个和第二个。 我只需要在第二个。

3)如何在数据库中提交此数据?

希望你理解我的问题。

在此处输入图像描述

代码 视图

 
- +
+ Add

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('
-+
- Remove
'); } }); $(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;} } 

您似乎还有删除/添加操作的错误。 如果您有添加4项的情况,那么

  • 1(初始)
  • 2,3,4(动态生成)

然后决定删除2.你将x减少一个,女巫给你x=3和行1,3,4 。 如果现在,你添加一个新项目,你得到x+1 ,所以收集1,3,4,4

为了避免这种模式,使用当前时间(在add执行时)生成行的标识符可能是个好主意。

由于您正在处理“add_row”div中包含的HTML,因此可以将其用作指示对象。 您可以获取.parents(“。add_row”),然后选择.find(’。single_price’)或.find(’。total_p_price’),而不是使用增加/减少点击事件收到的“n”值。容易

另一个方法是使用“data-”参数或类,允许您轻松识别您工作的当前行。 我可能会使用类似的东西:

 
- +
- Remove

(我简化了一点html以便轻松找到我的更改)通过这个和jquery,你可以立即找到你想要的所有数据并更新它$(’。single_price data [time =“1540021654”]’)。val()会给你这个结果,只有它。

另一个错误是,你正在生成一个id静态的按钮,比如这是一个错误,因为id应该是唯一的。

 $("body").on('keyup', 'input[id^=total_p_price]', function() { //$('input[id^=single_price]').prop('disabled', true); var that = $(this); // CHANGE HERE var total_p_price= that.val(); var qty_number = that.siblings().find("input[id^=number]").val(); console.log(qty_number); }); 
  
- +
+ Add
- +
+ Add