【问题标题】:how to change value of dynamically created input elements javascript/jquery如何更改动态创建的输入元素的值 javascript/jquery
【发布时间】:2019-01-02 02:33:41
【问题描述】:

我正在创建一个动态生成销售项目的表单,我已经可以从动态创建的元素中调用 javascript 中的函数,但是随着值的更改,我需要更改其他值。

显然,我无法设置动态创建输入的值

到目前为止我所做的,这是创建动态字段的地方

<table id="myTable" class="table table-bordered">
<thead>
    <tr>
        <th>Supplier</th>
        <th>Stock Item</th>
        <th>Quantity</th>
        <th>Sales Price</th>
        <th>Total</th>
        <th>Action</th>
    </tr>
</thead>
<tbody>
<tr>
    <td>
        <select name="Supplier_Number[]" class="form-control " id="Supplier_Number">
<option value="" selected="selected">Select</option>
<option value="Supplier-00006">test test 3</option>
<option value="Supplier-0005">test test</option>
<option value="Supplier-000007">test test 5</option>
</select>
    </td>
    <td>
        <select name="Stock_Number[]" class="form-control " id="Stock_Number" onchange="getStockData(event, 1);">
<option value="" selected="selected">Select</option>
<option value="Stock-00001">test 1</option>
<option value="Stock-00002">test</option>
<option value="Stock-000013">another test</option>
<option value="Stock-000014">another test 2</option>
</select>
    </td>
    <td>
        <input type="text" name="Quantity[]" id="Quantity1" onchange="getTotal(1)">
    </td>
    <td>
        <input type="text" name="Sales_Price[]" id="Sales_Price1">

        <input type="hidden" name="Purchase_Price[]" id="Purchase_Price1">
    </td>
    <td>
        <input type="text" name="Total[]" id="Total1">

    </td>
</tr>
</tbody>
</table>

这就是动态元素的样子

<tr>
<td>
    <select name="Supplier_Number[]" class="form-control " id="Supplier_Number2">
<option value="" selected="selected">Select</option>
<option value="Supplier-00006">test test 3</option>
<option value="Supplier-0005">test test</option>
<option value="Supplier-000007">test test 5</option>
</select>
</td>
<td>
    <select name="Stock_Number[]" class="form-control " id="Stock_Number2" onchange="getStockData(event,2);getTotal();">
<option value="" selected="selected">Select</option>
<option value="Stock-00001">test 1</option>
<option value="Stock-00002">test</option>
<option value="Stock-000013">another test</option>
<option value="Stock-000014">another test 2</option>
</select>
</td>
<td>
    <input type="text" name="Quantity[]" class="Quantity2" onchange="getTotal(2)">
</td>
<td>
    <input type="text" name="Sales_Price[]" class="Sales_Price2">

    <input type="hidden" name="Purchase_Price[]" class="Purchase_Price2">
</td>
<td>
    <input type="text" name="Total[]" class="Total2">

</td>
<td><input type="button" value="Delete"></td>
</tr>

这就是我尝试更改动态元素值的方式

var sales_price = 'Sales_Price'+count;
var Total = 'Total'+count;
var Purchase_Price = 'Purchase_Price'+count;
var Quantity = 'Quantity'+count;
$('#'+sales_price).prop('value', 'something');
$('#'+Total).prop('value', '0');
$('#'+Quantity).prop('value', '0');
$('#'+Purchase_Price).prop('value', 'something');

问题:

无法设置动态元素值。任何帮助将不胜感激。

【问题讨论】:

  • 你可以试试 $('#'+sales_price).val("A value");有效吗?
  • 没有不工作。输入是动态添加的,所以我必须在上一个选择选项上更改某些值时向该输入添加一些数据

标签: javascript jquery input dynamic


【解决方案1】:

您的动态创建的输入没有 id 属性,我认为您不小心将其分配给类

那么您可以在修复 id 问题后使用此代码更改输入值

var i = 1 //this is the index of added item

  $('#sales_price'+String(i)).val( 'something');
  $('#Total'+String(i)).val('0');
  $('#Quantity'+String(i)).val( '0');
  $('#Purchase_Price'+String(i)).val( 'something');

【讨论】:

  • 您必须编辑元素 id 以匹配选择器,例如"sales_price1","Total1","Quantity1","Purchase_Price1" 等每一行
  • 请再次阅读问题,我已经编辑了问题,但仍然无法正常工作
  • 你动态创建的输入没有 id 属性 我想你分配它不小心上课了
  • 这是一个愚蠢的错误,您能否更新您的答案指出问题,以便我可以使您的答案正确
猜你喜欢
  • 2020-12-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-03-03
  • 1970-01-01
相关资源
最近更新 更多