doupu0619 2018-08-03 03:00
浏览 46

动态输入字段值不使用正确的值计算

I am using Codeignator. My HTML output which is correct for me.

enter image description here Now What I am doing is, the user will enter the name of the medication, no of pills and amount so that based on the amount it will calculate the price and display it. Formula is $single_price=$total_amount/$qty_number;

enter image description here

Above image I added medication name="SIPLA", no of pills=3 and amount=30. It will calculate it and display the single price=10.00

Everything is working perfectly til now.

Let's talk about the "ADD" button. If any user wants more then one medication then he/she should click on "ADD" button and it will display the same field as above.

I did the same process, I added a medication name="ZOCON 400", no of pills=4 and amount=60. It calculating and displaying the single price=20.00 which is wrong it should be displayed single price=15.00.

1) Why I am getting single price=20.00 because it's talking no of pills=3 which is added in the first medication. So it's talking the first one quantity. It should talk no of pill=4

2) The calculation of the single price is also displaying in both fields. first one as well as second one. I need only in the second one.

3) How to submit this data in the database?

Hope you understand my issue.

enter image description here

Code View

<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 and 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); 
        }
      });
});

});

Controller

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;
}

Model

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;}

}
  • 写回答

2条回答 默认 最新

  • drzrdc1766788 2018-08-03 03:15
    关注

    $("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);
    });
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <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><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>

    I can answer first two, for the third question, there is not much info to go on. For 3, are you getting any error? First two questions have similar issue

    The issue is in the following syntax $('input[id^=single_price]').val(html);

    What that does is it selects all the inputs in the dom that match the criteria. In your case you have two inputs that match the criteria and hence both are updated. You need to choose only the one that are in the same row. You can do that using the siblings function that Jquery provides. Change your function in the following way

    $("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('input[id^=number]').val(); // selects only the one closest to it
        $.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);
              that.siblings('input[id^=single_price]').val(html); 
            }
          });
    });
    
    </div>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 想问一下树莓派接上显示屏后出现如图所示画面,是什么问题导致的
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号