javascript 使用javascript为输入元素设置动态不同的id属性
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13339996/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Setting dynamically distinct id attribute using javascript for input element
提问by Chinmaya Kumar Panda
I am facing a problem in setting distinct ids for dynamically added input boxes in a table. The problem is:
我在为表格中动态添加的输入框设置不同的 id 时遇到问题。问题是:
I am inserting new rows when the user clicks on "Add new Row". Now I have to set distinct id for the input box. I have taken a static variable and increasing its value every time user clicks on add new row button. Then I am appending its value to id value lets say temp (i.e. ids will be temp1
, temp2
and so on). I have taken a variable xyz as
var xyz = "temp" + i
(where i is the counter)
and now I have to assign xyz
to the id using setattribute
.
But as values in setattribute
can only be literals, how can I use a variable in place of the value?
当用户单击“添加新行”时,我正在插入新行。现在我必须为输入框设置不同的 id。每次用户单击添加新行按钮时,我都会采用一个静态变量并增加其值。然后我将它的值附加到 id 值让我们说 temp (即 ids 将是temp1
,temp2
等等)。我已经将变量 xyz 作为
var xyz = "temp" + i
(其中 i 是计数器),现在我必须xyz
使用setattribute
. 但是由于值setattribute
只能是文字,我如何使用变量代替值?
If you have any other methods, please let me know.
如果您有任何其他方法,请告诉我。
回答by gdoron is supporting Monica
回答by Declan Cook
setAttribute
allows for variables to be used as the value parameter.
setAttribute
允许将变量用作值参数。
eg:
例如:
var ele = document.createElement('div')
var id = 'temp1'
ele.setAttribute('id', id)
would result in:
会导致:
<div id="temp1"></div>
回答by thinklinux
element.setAttribute("id", xyz);
Where element is your input.
element.setAttribute("id", xyz);
其中 element 是您的输入。
回答by Oleg
First of all setattribute
is an undefined property of an element because of case-sensitivity while setAttribute
, of course, is available.
首先setattribute
是元素的未定义属性,因为它区分大小写setAttribute
,当然,它是可用的。
Second, you are not absolutely required to use setAttribute
at all. You can simply modify the id
property to achieve the very same effect:
其次,您根本不需要使用setAttribute
。您可以简单地修改该id
属性以达到相同的效果:
var el = document.createElement('div'), // your element
staticPart = 'myUniqId', // your static part of a unique id
i = 0; // your uniqueness
el.id = staticPart + i; // assign unique id to the element
// el.setAttribute('id', staticPart + i); // does the same, but more verbose
// Let's check if it worked:
el.getAttribute('id'); // "myUniqId0"
el.id; // "myUniqId0"
Third, where did you see that
三、你在哪里看到的
values in setattribute can only be literals
setattribute 中的值只能是文字
The spec says that setAttribute
accepts two parameters, name
and value
of type DOMstring. You can pass it any value you want. Mind that it will be converted to String:
规范说它setAttribute
接受两个参数,name
并且value
是DOMstring类型。你可以传递任何你想要的值。请注意,它将被转换为字符串:
el.id = {a: 'b'};
el.id; // "[object Object]"
See http://www.w3.org/TR/DOM-Level-2-Core/core.html#ID-F68F082and https://developer.mozilla.org/en-US/docs/DOM/element.setAttribute
请参阅http://www.w3.org/TR/DOM-Level-2-Core/core.html#ID-F68F082和https://developer.mozilla.org/en-US/docs/DOM/element.setAttribute
回答by pravin sharma
for(;i<7;i++)
{document.getElementById("test").innerHTML +='<form id="form'+f+++'" method="get"><input type="text" id="in'+i+++'" size="12%"><input type="text" id="in'+i+++'" size="12%" ><input type="text" id="in'+i+++'"size="12%"><input type="text" id="in'+i+++'"size="10%" ><input type="text" id="in'+i+++'"size="10%"><input type="text" id="in'+i+++'" size="10%"><input type="text" id="in'+i+++'"size="12%"></form>'
}