[英]how to change value of dynamically created input elements javascript/jquery
I am creating a form that generates sales items dynamically, i can already call a function in javascript from that dynamically created elements but as a value is changed I need to change other values. 我正在创建一个可以动态生成销售商品的表单,我已经可以从该动态创建的元素中使用javascript调用函数,但是随着值的更改,我需要更改其他值。
Apparently, I cannot set value of dynamically created input 显然,我无法设置动态创建的输入的值
What I've done so far, this is where the dynamic field is created 到目前为止,我所做的就是创建动态字段的地方
<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>
This is how the dynamic elements looks 这就是动态元素的外观
<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>
This is how I am trying to change dynamic element value 这就是我试图更改动态元素值的方式
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');
Problem: 问题:
The dynamic elements value cannot be set. 无法设置动态元素值。 Any help will be appreciated.
任何帮助将不胜感激。
your dynamic created inputs dosen't have id attribute I think you assign it to class accidentally 您动态创建的输入没有id属性,我认为您不小心将其分配给了班级
then you can use this code to change inputs values after fix id's issue 那么您可以使用此代码在发布修订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');
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.