【问题标题】:Dynamic input field values are not calculating the with the right value动态输入字段值未使用正确的值计算
【发布时间】:2018-08-03 03:00:53
【问题描述】:

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

现在我正在做的是,用户将输入药物的名称,药丸的数量和数量,以便根据数量计算价格并显示它。公式为$single_price=$total_amount/$qty_number;

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

到目前为止一切正常。

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

我做了同样的过程,我添加了medication name="ZOCON 400", no of pills=4amount=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


【解决方案1】:

您的删除/添加操作似乎也有错误。 如果您有添加 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-”参数或类,让您轻松识别当前正在工作的行。 我可能会使用类似的东西:

<div class="custom_fields" data-time="1540021654"> <!-- current time on the dynamically added row -->
  <input type="text" name="medication_name[]" data-time="1540021654" class="medication_name">
  <span class="value-button" data-time="1540021654" onclick="decreaseValue" value="Decrease Value">-</span>
  <input type="number" data-time="1540021654" class="qty_member" value="0" name="qty_member[]" />
  <span class="value-button" data-time="1540021654" class="increase_name" onclick="increaseValue">+</span>
  <br />
  <input type="text" class="form_control" data-time="1540021654" name="single_price[]" class="single_price"  />    
  <input type="text" class="form_control" data-time="1540021654" name="total_p_price[]" class="total_p_price" /> 
  <div class="btn_row remove_field"> 
  <span> - </span> Remove  </div>
</div>

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

另一个错误是,您正在生成带有静态 ID 的按钮,例如 &lt;span class="value-button" id="increase" onclick="increaseValue('+ x +')" value="Increase Value"&gt; 这是一个错误,因为 id 应该是唯一的。

【讨论】:

  • 哦!我没有注意到你是对的。如果我添加 1(初始)和动态(2,3,4,5),如果我删除 3,则再添加一个动态字段,这次动态字段是(2,4,5,5)。这是完全错误的。给我时间来实现你的代码
  • 上一期我更正了。 id 现在是唯一的。我只是想知道您是如何动态添加 data-time="1540021654" 的?
  • 这方面还有什么帮助吗?请
  • 要动态添加数据,请使用 Jquery 语法。 $('input.single_price').data('time',154001234) 应该可以工作
  • 我应该在哪里添加上面的脚本?我的意思是,在哪个函数中?
【解决方案2】:

$("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>

我可以回答前两个问题,对于第三个问题,没有太多信息可以继续。对于3,你有任何错误吗?前两个问题有类似的问题

问题在于以下语法$('input[id^=single_price]').val(html);

它的作用是选择 dom 中与条件匹配的所有输入。在您的情况下,您有两个符合条件的输入,因此都更新了。您只需选择同一行中的一个。您可以使用 Jquery 提供的兄弟功能来做到这一点。通过以下方式更改您的功能

$("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); 
        }
      });
});

【讨论】:

  • 我试过你的代码,但它没有显示计算值。 that.siblings('input[id^=single_price]').val(html);它没有在视图中显示输出
  • 您是否还包括了行 var that = $(this); ?
  • 是的,我包括在内。
  • 能否请您在成功回调中控制台记录 html 的值?我试过了,代码有效。
  • ,console.log(that.siblings('input[id^=single_price]').val(html);) 我越来越喜欢prntscr.com/ke40h0
猜你喜欢
  • 1970-01-01
  • 2021-03-27
  • 1970-01-01
  • 1970-01-01
  • 2018-10-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多