簡體   English   中英

asp.net js controlID動態增加

[英]asp.net js controlID increase dynamically

我想每次單擊按鈕時都增加ASP controlID。 就像添加更多電話號碼一樣。 每次添加新的asp:textbox時,都應創建新的ID。 添加更多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); } 

有人可以幫忙嗎?

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);

    }

將其更改為此代碼

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