[英]How do you add a list id to a newly created HTML element?
How do I dynamically add text inputs to a table while assigning the same list id? 如何在分配相同的列表ID时动态地向表中添加文本输入? My code is currently:
我的代码目前是:
function addspeakers(){
for(j=0; j<10;j++){
var cellid='inp'+cellnum;
input=document.getElementById('sprow').appendChild(document.createElement('input'));
input.placeholder='Country '+cellnum;
input.id='inp'+cellnum;
// add to list input.addtolist(countries);
cellnum++;
}
return
}
all it does is add 10 text inputs to a list however these lists have drop down menus... This is the code for the datalist: 它所做的只是将10个文本输入添加到列表中,但是这些列表具有下拉菜单...这是数据列表的代码:
<datalist id='countries'>
and the text input elements should be created looking like this: 应该创建文本输入元素,如下所示:
<table id='splist'>
<tr>
<td id='sprow'>
<input list='countries' placeholder='Country 1' id='inp1'>
</td>
</tr>
</table>
What do I need to add it to the list id? 我需要将它添加到列表ID中?
You simply need to assign it like your other variables: 您只需像其他变量一样分配它:
input.list='countries';
Also, you never use cellid
, and your return
is unnecessary ;) 此外,你永远不会使用
cellid
,你的return
是不必要的;)
Hope this helps! 希望这可以帮助!
Use setAttribute
to set the property on the element. 使用
setAttribute
设置元素的属性。
input.setAttribute('list', 'countries');
Also since list
is a not a default attribute of the element, but a custom attribute. 此外,由于
list
不是元素的默认属性,而是自定义属性。
You can use data-*
( which is custom attributes) 你可以使用
data-*
(这是自定义属性)
<input data-list='countries'
input.setAttribute('data-list', 'countries');
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.