简体   繁体   English

asp.net js controlID动态增加

[英]asp.net js controlID increase dynamically

I want to increase ASP controlID each time I click button. 我想每次单击按钮时都增加ASP controlID。 Like adding more phone numbers. 就像添加更多电话号码一样。 Each time new asp:textbox is added, new ID should be created. 每次添加新的asp:textbox时,都应创建新的ID。 Adding more div/textbox is working fine, but ID of each textbox is same. 添加更多div / textbox效果很好,但是每个文本框的ID相同。

 function GetDynamicMobileNo(value) { return '<div><asp:TextBox ID="txtAddMoreNumber" runat="server" CssClass="form-control" MaxLength="11" Placeholder="Mobile No"></asp:TextBox></div></br>'; } function AddMobileNo() { var div = document.createElement('DIV'); div.innerHTML = GetDynamicMobileNo(""); document.getElementById("MobileNoContainer").appendChild(div); } 

Anyone can help? 有人可以帮忙吗?

Id is same because you are generating html with the same Id. ID相同,因为您使用相同的ID生成HTML。

//Here is your problem
function GetDynamicMobileNo(value) {
        return '<div><asp:TextBox ID="txtAddMoreNumber" runat="server" CssClass="form-control" MaxLength="11" Placeholder="Mobile No"></asp:TextBox></div></br>';
    }

    function AddMobileNo() {        
        var div = document.createElement('DIV');
        div.innerHTML = GetDynamicMobileNo("");
        document.getElementById("MobileNoContainer").appendChild(div);

    }

Change it to this code 将其更改为此代码

function GetDynamicMobileNo(value, id) {
        return "<div><asp:TextBox ID=\" + id + \" runat=\"server\" CssClass=\"form-control\" MaxLength=\"11\" Placeholder=\"Mobile No\"></asp:TextBox></div></br>";
    }

    function AddMobileNo() {        
        var div = document.createElement('DIV');
        div.innerHTML = GetDynamicMobileNo("", someRandomIdGenerator());
        document.getElementById("MobileNoContainer").appendChild(div);

    }

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

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