Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/228.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 分割值计算不正确_Javascript_Php_Jquery_Ajax - Fatal编程技术网

Javascript 分割值计算不正确

Javascript 分割值计算不正确,javascript,php,jquery,ajax,Javascript,Php,Jquery,Ajax,这是有关的。我正在添加行项目,根据选择的价格、数量、折扣和税收自动计算 仅针对税,我将值传递为1_2,即id和税率。因此,对于第一行项目,我得到了一个适当的税值,但从第二行开始,无论我选择什么税,它都取第一个项目的税值。我看不出我做错了什么 这是我的HTML,通过它我可以选择值: <td> <select name="tax[]" class="form-control tax" id="tax_1" style="width:80px;">

这是有关的。我正在添加行项目,根据选择的价格、数量、折扣和税收自动计算

仅针对税,我将值传递为1_2,即id和税率。因此,对于第一行项目,我得到了一个适当的税值,但从第二行开始,无论我选择什么税,它都取第一个项目的税值。我看不出我做错了什么

这是我的HTML,通过它我可以选择值:

    <td>
        <select name="tax[]" class="form-control tax" id="tax_1" style="width:80px;">
            <option value="">Tax</option>
<?php 
    $s1 = mysqli_query($con, "select * from taxes");
    while($s2 = mysqli_fetch_array($s1)) { 
        $options .= "<option value='". $s2['tax_id']."_".$s2['rate']."'>"
                    .$s2['name'].'-'.$s2['rate'] . "</option>"; 
?>
            <option value="<?php echo $s2['tax_id']."_".$s2['rate']; ?>">
                <?php echo $s2['name'].'-'.$s2['rate']; ?></option>
<?php
    }  
?>
        </select>
    </td>                        
</tr>  
我的剧本:

  $(".addmore").on('click', function() {
    count = $('table tr').length - 1;
    var data = "<tr><td><input type='checkbox' class='case'/></td><td><input class='form-control' type='text' id='productname_" + i + "' name='productname[]'/></td><td><input class='form-control' type='text' id='productcode_" + i + "' name='productcode[]'/></td> <td><textarea class='form-control' id='description_"+ i + "' name='description[]'></textarea></td><td><select class='form-control uom' id='uom_" + i + "' name='uom[]'><option value=''>UOM</option>" + options1 + "</select></td><td><input class='form-control price' type='text' id='price_" + i + "' name='price[]'/></td><td><select class='form-control tax' id='tax_" + i + "' name='tax[]'><option value=''>Tax</option>" + options + "</select></select></td><td><input class='form-control quantity' type='text' id='quantity_" + i + "' name='quantity[]'/></td><td><input class='form-control discount' type='text' id='discount_" + i + "' name='discount[]'/></td><td><input class='form-control amount' type='text' id='amount_" + i + "' name='amount[]'/></td><td><input class='form-control tamount' type='text' id='tamount_" + i + "' name='tamount[]'/></td></tr>";
    $('table').append(data);
    row = i;
    $('#productname_' + i).autocomplete({
      source: function(request, response) {
        $.ajax({
          url: 'ajax.php',
          dataType: "json",
          method: 'post',
          data: {
            name_startsWith: request.term,
            type: 'items_table',
            row_num: row
          },
          success: function(data) {
            response($.map(data, function(item) {
              var code = item.split("|");
              return {
                label: code[0],
                value: code[0],
                data: item    
              }
            }));
          }
        });
      },

  $('body').on('change', '.quantity,.price,.discount,.tax', function() {
    var tr = $(this).parent().parent();
    var qty = tr.find('.quantity').val();
    var price = tr.find('.price').val();
    var taxVal= $('.tax').val();
    var tax_id=taxVal.split('_')[0];
    var rate=taxVal.split('_')[1];
  // Here from 2nd line item i am getting 1st line items value for tax. 
    var dis = tr.find('.discount').val();
    var amt = (qty * price) - (qty * price * dis) / 100;
    var tax1 = (amt * (rate / 100));

    tax1 = Number((tax1).toFixed(2));
    tr.find('.tamount').val(tax1);
    ttotal();
    tr.find('.amount').val(amt);
    total();
    //tr.find('.ttotal1').val();
  });
我明白了。 这是因为您正在选择.tax选择器,如下所示:

var taxVal=$'.tax'.val

你应该用$this来抓取它。因为你这样做,它总是抓住第一个

请看这个例子

简言之: 元"税";;将选择DOM中具有.tax类的任何元素。 当您使用任何类型的绑定时,这将是触发绑定更改事件的元素

因此,通过抓取$this,您将抓取实际已更改的元素。通过获取$'.tax',您只需获取DOM中的任何.tax元素

希望这有助于:

示例html p、 在您自己的代码中,您已经为其他字段获取parentrow以查找它们,那么为什么不为.tax也这样做呢

tr.find'.tax'.val

因此,对于第一行项目,我得到了一个适当的税值,但是从 第二步,无论我选择哪种税,它都会接受第一项的税 价值观

据我所知,您没有使用当前的对象值。您总是引用类名。因为,每个输入都有ID,ID之间用u分隔。您可以获取该特定输入字段的ID,并可以拆分以获取该字段的准确/当前ID。使用此当前ID,您也可以找到其他输入值

更新代码


请只发布代码中工作不正常的部分。@ibrahimmahrir:编辑了我的代码一个问题:此函数$.addmore的关闭位置在哪里。单击时,函数{。此函数是否包括$'body'。更改时,'.quantity、.price、.折扣、.tax',函数{这个函数也是??谢谢,但我把这些行放在$'body'后面。关于'change','quantity',price',折扣,'tax',函数{,没有结果。我不明白我做错了什么。非常感谢。我用了tr.find'.tax'.val;,它工作得很好!谢谢娜娜·帕蒂卡:我知道了。
<select class="tax" id="tax">
  <option value="1_2">1_2</option>
  <option value="1_3">1_3</option>
  <option value="2_2">2_2</option>
</select>

<select class="tax" id="tax">
  <option value="3_2">3_2</option>
  <option value="3_3">3_3</option>
  <option value="3_2">3_2</option>
</select>
$('.tax').on('change', function() {

  var $changedInput = $(this);
  var value = $changedInput.val();
  var aValues = value.split('_');
  var tax_id = aValues[0];
  var rate = aValues[1];

  alert("tax_id: " + tax_id + " Rate:" + rate );
});
$('body').on('change', '.quantity,.price,.discount,.tax', function() {

    var tr = $(this).parents('tr');
    var id_clicked =  $(this).attr('id');
    var current_id = id_clicked.split("_")[1];

    var qty = tr.find("#quantity_"+current_id).val();
    var dis = tr.find("#tax_"+current_id).val();
    var price = tr.find("#price_"+current_id).val();
    var taxVal= tr.find("#tax"+current_id).val();
    var rate = taxVal.split('_')[1];

    var amt = (qty * price) - (qty * price * dis) / 100;
    var tax1 = (amt * (rate / 100));
    tax1 = Number((tax1).toFixed(2));

    tr.find('#tamount_'+current_id).val(tax1);
    tr.find('#tamount_'+current_id).val(amt);
  });