![](/img/trans.png)
[英]Dynamic creation of list and adding HTML Tags in the each newly created element
[英]Dynamic id for HTML element which is getting created newly
我正在用选项框替换表格> td 内的跨度。 我想在创建选项框时分配一些具有动态值的唯一 id。 如何实现这一目标?
已生成唯一 ID,我想在创建时在 Html 中设置此唯一 ID。
<td id="tdTest">
<span id="spanId" class="connectedSlot" style="color:rgb(27,99,173)">Enabled</span>
</td>
$("#"+spanId).replaceWith(function () {
return "<select class='form-control' id=<<DYNAMIC ID>> style='width:200px;'><option value='0'>Enabled</option><option value='1'>Disabled</option></select>";
});
例如,我将从变量中获取一些值并将其分配给变量,然后将该变量分配给 id。
var myId = "OptionId"+test;
select class='form-control' id=myId style='width:200px;'>
谢谢
在字符串中使用变量,如下所示:
var unqiueID = "myID123", ret = "";
ret = "<select class='form-control' id='" + unqiueID +"' style='width:200px;'>";
确保分配唯一值的一种可能方法是跟踪所有可用的 id 并分配一个不在我们列表中的 id。
getUniqueId()
是一个始终返回唯一 ID 的函数。
function getUniqueId() {
//get all the ids in this document
var ids = new Array();
$('[id]').each(function() { //Get elements that have an id=
ids.push($(this).attr("id")); //add id to array
});
//give some random value to uniqueId
//if this value is in ids array, then assign a different id and recheck the condition
var uniqueId;
while(true) {
uniqueId = 'some-prefix-' + Math.floor(Math.random() * 10000);
if($.inArray(uniqueId , ids) == -1) {
break;
}
}
return uniqueId;
}
自发布以来已经有几年了,接受的解决方案对我不起作用。 这是这样做的语法:
[id]="unqiueID"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.