[英]How to set dynamic input name which generated dynamically using jquery live()
I am new in JavaScript, I have a html table with two text fields and add button and now I write a little jquery script which generate two more text fields when click on add button all this work success but my problem is how to put different names and ids of these dynamically generated fields. 我是JavaScript的新手,我有一个带两个文本字段和添加按钮的html表,现在我写了一个小jQuery脚本,当单击添加按钮时,该脚本又生成了两个文本字段,所有这一切都成功了,但是我的问题是如何输入不同的名称和这些动态生成的字段的ID。 My codes are below. 我的代码如下。
<table width="50%" border="1" cellspacing="0" cellpadding="0" class="table">
<tr>
<td>ACC_CODE</td>
<td>ACC_NAME</td>
<td>ACTION</td>
</tr>
<tr>
<td> <input type="text" class="acc_code" name="acc_code" id="acc_code"/></td>
<td> <input type="text" class="click" name="parentinput" id="parentinput" /></td>
<td> <input type="button" value="Add New" class="addNew" name="addNew" /></td>
</tr>
</table>
$(document).ready(function() {
$('.addNew').live({
click: function(){
$('.table tr:last').after('<tr><td><input type="text" class="acc_code" name="acc_codes" id="acc_codes"/></td><td><input type="text" class="click" name="parentinputs" id="parentinputs" /></td><td><input type="button" value="Add New" class="add" name="add" /></td></tr>');
});
});
Please any help how to generate different names and ids of these fields. 请任何帮助如何生成这些字段的不同名称和ID。
First of all .live is depreciated since jq 1.7 Use .delegate 首先,从jq 1.7开始,.live被贬值。使用.delegate
so.. 所以..
$(document).ready(function() {
var idIndex=1;
$(document).delegate("click",".addNew",function() {
$('.table tr:last').after('<tr><td><input type="text" class="acc_code" name="acc_codes" id="acc_codes_'+idIndex+'"/></td><td><input type="text" class="click" name="parentinputs" id="parentinputs_'+idIndex+'" /></td><td><input type="button" value="Add New" class="add" name="add" /></td></tr>');
idIndex++;
});
});
I have a suggestion that may work fine, 我有个建议可能很好用,
why not make the input field to be an array, 为什么不将输入字段设为数组,
so when you submit it, it will be a group of input group together 因此,当您提交它时,它将是一组输入组
or you can easily make the name id's to be base on tr index 或者您可以轻松地使名称id基于tr索引
like name_1 and id_1, name_2 and id_2 如name_1和id_1,name_2和id_2
I would use on() ( and off() ). 我会使用on()(和off())。 You set it to the parent container (in this case the table) and listen to the class 'addNew'. 您将其设置为父容器(在本例中为表),然后侦听“ addNew”类。 Any time a new item with class="addNew" is added to the table, it will be active. 每当将具有class =“ addNew”的新项目添加到表中时,它将处于活动状态。
<table width="50%" border="1" cellspacing="0" cellpadding="0" class="table">
<tr>
<td>ACC_CODE</td>
<td>ACC_NAME</td>
<td>ACTION</td>
</tr>
<tr>
<td> <input type="text" class="acc_code" name="acc_code" id="acc_code"/></td>
<td> <input type="text" class="click" name="parentinput" id="parentinput" /></td>
<td> <input type="button" value="Add New" class="addNew" name="addNew" /></td>
</tr>
</table>
<script>
$(document).ready(function() {
$('.table').on('click', '.addNew', function() {
$('.table tr:last').after('<tr><td><input type="text" class="acc_code" name="acc_codes" id="acc_codes"/></td><td><input type="text" class="click" name="parentinputs" id="parentinputs" /></td><td><input type="button" value="Add New" class="addNew" name="add" /></td></tr>');
});
});
</script>
But notice: in your code you did this: 但请注意:在您的代码中,您这样做:
<input type="button" value="Add New" class="add" name="add" />
Of course this will not work; 当然这是行不通的。 it has to be class="addNew" 它必须是class =“ addNew”
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.