简体   繁体   English

如何设置使用jquery live()动态生成的动态输入名称

[英]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. 我的代码如下。

HTML Code HTML代码

 <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>&nbsp;<input type="text" class="acc_code" name="acc_code" id="acc_code"/></td>
    <td>&nbsp;<input type="text" class="click" name="parentinput"  id="parentinput" /></td>
     <td>&nbsp;<input type="button" value="Add New" class="addNew" name="addNew" /></td>
  </tr>
</table>

Jquery Code jQuery代码

$(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>&nbsp;<input type="text" class="acc_code" name="acc_code" id="acc_code"/></td>
    <td>&nbsp;<input type="text" class="click" name="parentinput"  id="parentinput" /></td>
    <td>&nbsp;<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.

相关问题 如何使用动态生成的html输入类型实时填充div - how to live populate div using dynamically generated html input types 为jquery中动态生成的输入文本设置值 - Set Values for dynamically generated input text in jquery 使用动态ID和名称验证动态生成的输入框 - Validate dynamically generated input box with dynamic ID and name 如何设置动态添加输入的名称值? javascript jQuery - How to set name value of dynamic adding input ? javascript jquery 如何使用Javascript / Jquery动态添加输入字段的名称属性 - How to add name attribute of input field dynamically using Javascript/Jquery 在JavaScript中动态生成的输入名称 - Dynamically generated input name in JavaScript 如何在asp.net中使用C#将使用jquery flot动态生成的图表导出到Excel中? - How to export charts which are dynamically generated by using jquery flot into Excel using C# in asp.net? 使用jQuery序列化表格外的动态生成的HTML输入元素 - serialize the dynamically generated HTML input elements outside the form using jQuery 即使使用 Jquery,也可以在 onClick 上动态生成输入值 - Get dynamically generated input value on an onClick even using Jquery 如何提取在动态生成的输入框中输入的值,然后使用jquery将其传递给控制器​​? - How can I extract the values entered in the input boxes generated dynamically and pass it to controller using jquery?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM