简体   繁体   English

将id属性添加到html输入

[英]add id attribute to html input

I am dynamically inserting new html text inputs through a button, however when i create them and assign an ID, it assigns the ID to the H4 text and label, but not the actual input element. 我通过按钮动态插入新的html文本输入,但是当我创建它们并分配ID时,它会将ID分配给H4文本和标签,而不是实际的输入元素。 I'm using Jquery to create the elements.. 我正在使用Jquery创建元素。

            $("<h4><span style=\"color: #006085\">Target " + counter + ":</span> Enter Name :</h4><label><span>Name :</span><input type='text' value='' /></label>")
         .attr("id", "targetname" + counter)
         .insertBefore("#elementtarget");

        $("<h4><span style=\"color: #006085\">Target " + counter + ":</span> Enter Return :</h4><label><span>Return :</span><input type='text' value='' /></label>")
         .attr("id", "targetreturn" + counter)
         .insertBefore("#elementtarget");

I know that the input is technically a child, but if i try to set the attribute of the child the H4 text disappears. 我知道输入从技术上来说是孩子,但是如果我尝试设置孩子的属性,H4文本就会消失。

I know that the input is technically a child... 我知道输入内容从技术上来说还是个孩子...

Nothing "technical" about it, it's a child. 没什么“技术上的”,只是个孩子。 You can still set its id , though: 不过,您仍然可以设置其id

$("<h4><span style=\"color: #006085\">Target " + counter + ":</span> Enter Name :</h4><label><span>Name :</span><input type='text' value='' /></label>")
.find("input").attr("id", "targetname" + counter).end()
.insertBefore("#elementtarget");

(And similarly for the next block.) There, after creating the structure, I use .find("input") to find the input within it, then assign the id , then use end so we get back to the original jQuery object (the h4 and its descendants) so we can use insertBefore on it. (和下一个块类似。)在那里,在创建结构之后,我使用.find("input")在其中查找input ,然后分配id ,然后使用end以便返回到原始的jQuery对象( h4及其后代),因此我们可以在其上使用insertBefore

Alternately, you could add the id after inserting the structure: 或者,您可以在插入结构后添加id

$("<h4><span style=\"color: #006085\">Target " + counter + ":</span> Enter Name :</h4><label><span>Name :</span><input type='text' value='' /></label>")
.insertBefore("#elementtarget")
.find("input").attr("id", "targetname" + counter);

...which means you don't need end() anymore. ...这意味着您不再需要end()

That's because of the order of your chaining. 那是因为链接的顺序。 If you find and add attribute before appending it to the DOM, you'll end up adding only the input (because of the find). 如果在将属性附加到DOM之前找到并添加属性,则最终只会添加输入(由于查找)。

Just append the element then change your id : 只需追加元素,然后更改您的id即可:

$("<h4><span style=\"color: #006085\">Target " + counter + ":</span> Enter Return :</h4><label><span>Return :</span><input type='text' value='' /></label>")
.insertBefore("#elementtarget")
.find('input')
.attr("id", "targetreturn" + counter);

How about setting the id on the html string? 如何在HTML字符串上设置ID?

    $("<h4><span style=\"color: #006085\">Target " + counter + ":</span> Enter Return :</h4><label><span>Return :</span><input type='text' value='' id='targetreturn" + counter + "'/></label>")
     .insertBefore("#elementtarget");

JsFiddle demo JsFiddle演示

PS: remember to parse the input fields before submitting; PS:记得在提交之前先解析输入字段; otherwise you will not get your data... ...or set also the "name" on the input field :p 否则您将无法获取数据...或在输入字段上也设置“名称”:p

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM