Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/76.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 将文本框的父名称和ID附加到新生成的文本框_Javascript_Jquery_Html - Fatal编程技术网

Javascript 将文本框的父名称和ID附加到新生成的文本框

Javascript 将文本框的父名称和ID附加到新生成的文本框,javascript,jquery,html,Javascript,Jquery,Html,我能够生成更多的文本框,供用户点击链接输入变量。我注意到的是,当生成新的文本框行时,它会生成nametext,id为id。但我希望它具有与原始文本框或父文本框相同的名称 <table width="80%" border="0" align="center" cellpadding="5" cellspacing="5"> <tr> <td bgcolor="#CCCCFF"><strong>4. VECHICLE INFORMATIO

我能够生成更多的
文本框
,供用户点击链接输入变量。我注意到的是,当生成新的文本框行时,它会生成
name
text,id为
id
。但我希望它具有与原始文本框或父文本框相同的名称

<table width="80%" border="0" align="center" cellpadding="5" cellspacing="5">
  <tr>
    <td bgcolor="#CCCCFF"><strong>4. VECHICLE INFORMATION</strong></td>
    <td bgcolor="#CCCCFF">&nbsp;</td>
    <td bgcolor="#CCCCFF">&nbsp;</td>
    <td bgcolor="#CCCCFF"><div align="right"><a class="add_more" href="#">Add More Vechicles</a></div></td>
  </tr>
  <tr id="more_vechicle">
    <td bgcolor="#FFFFFF"><input type="text" name="vechicle_name[]" id="vechicle_name" class="register-input" placeholder="Name of Vechicle" /></td>
    <td bgcolor="#FFFFFF"><input type="text" name="vechicle_type[]" id="vechicle_type" class="register-input" placeholder="Type of Vechicle" /></td>
    <td bgcolor="#FFFFFF"><input type="text" name="vechicle_registration[]" id="vechicle_registration" class="register-input" placeholder="Registration No." /></td>
    <td bgcolor="#FFFFFF"><input type="text" name="vechicle_color[]" id="vechicle_color" class="register-input" placeholder="Color of Vechicle" /></td>
  </tr>
  <tr>
    <td colspan="4" bgcolor="#FFFFFF"><div align="center">
      <input type="submit" name="button" id="button" value="Save &amp; Continue" />
    </div></td>
  </tr>
</table>

4。车辆信息
JS

$(文档).ready(函数()
{
$('.add_more')。单击(函数(){
变量id=$(':text')。长度;
$(“#更多车辆td”)。附加(“”);
});
});
使用
.each()
在TDs上循环。然后,您可以获取每个TD中的第一个输入,克隆它,并给它一个新ID

$('.add_more')。单击(函数(){
$('#更多车辆td')。每个(函数(){
var this_input=$(this.find(“.register input”);
var id=这些输入的长度;
var new_input=这些_input.first().clone(true).val(“”);
新输入.attr('id',函数(i,旧id){
返回旧的_id+id;
});
$(此).append(新输入);
})
});

4。车辆信息

您正在寻找的解决方案是jquery的clone()函数()。只需克隆
#更多车辆
,并将其插入“继续”按钮之前

$('.add_more').click(function() {
    $('#more_vechicle').clone()
      .attr('id', null) /* removing id attribute for the cloned
        * tr so there is only one elemnt with that id
        */
      .insertBefore('#append_anchor');
请注意,clone()的默认行为是不从父元素复制事件处理程序。 我还建议确保每辆车都有自己的tr。请参见以下内容:

您确定需要ID吗?如果您动态生成ID,您可能不需要它们,因为其他代码不会尝试通过ID访问它们。只需发送文本,当我向第一组行添加值并单击“添加更多”时,第一行中的值将显示在新的行中,我必须清除
$('.add_more').click(function() {
    $('#more_vechicle').clone()
      .attr('id', null) /* removing id attribute for the cloned
        * tr so there is only one elemnt with that id
        */
      .insertBefore('#append_anchor');