如何获取动态添加的下一个输入的值

时间:2017-02-20 07:53:23

标签: javascript jquery

我将数据附加到tbody,我通过ajax请求。在后端我打印这样的数据

echo "<tr>
        <td><input type='text'  name='sl_p_codes[]' id='p_code' value='{$data['p_code']}' ></td>
        <td><input type='text'  name='sl_products[]' id='qty' value='{$data['p_name']}' ></td>
        <td><input type='text' id='product_qty' name='sl_p_qty[]'  id='qt' value='01' size='2'></td>
        <td><input type='hidden'  name='sl_unit_cost[]'  id='product_unit_price'></td>
        <td><input type='text' id='product_total_cost' readonly name='sl_price[]' id='qty' value='{$data['p_price']}' ></td>
        <td class='center'><a href='#'  onclick='delrow(this);' class='btn btn-danger'>X</a></td>
    </tr>";

如果我更改id='product_qty'(输入字段),则应使用id='product_total_cost'

的值更新id='product_unit_price'(输入字段)的值

1 个答案:

答案 0 :(得分:1)

你的意思是这样的:

&#13;
&#13;
$("#product_qty").on("change", function() {
  $(this).closest("tr").find("#product_total_cost").val(
    (parseFloat($(this).closest("tr").find("#product_unit_price").val()) * parseInt($(this).val())).toFixed(2)
  );
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table>
  <tr>
    <td><input type='text' name='sl_p_codes[]' id='p_code' value='ABC011'></td>
    <td><input type='text' name='sl_products[]' id='qty' value='01'></td>
    <td><input type='number' id='product_qty' name='sl_p_qty[]' id='qt' value='01' size='2'></td>
    <td><input type='hidden' name='sl_unit_cost[]' id='product_unit_price' value="1.00"></td>
    <td><input type='text' id='product_total_cost' readonly name='sl_price[]' id='qty' value='1.00'></td>
    <td class='center'><a href='#' onclick='delrow(this);' class='btn btn-danger'>X</a></td>
  </tr>
</table>
&#13;
&#13;
&#13;