[英]Dynamically textbox creation using javascript
您好,我正在嘗試使用 for 循環動態創建文本框,並希望在所有文本框中實現日歷。 我的代碼如下:
for (var j = 1; j <= 5; j++) {
my_proj.innerHTML = my_proj.innerHTML +'<input type="text" name="txtStartDate" id="txtStartDate"+j runat="server" BackColor="#D6E4ED" BorderStyle="Inset" Width="10px" ></input>'
+'<a href="javascript:;" onclick="window.open(\'PopUp.aspx?textbox=txtStartDate+j\',\'cal\',\'width=250,height=225,left=270,top=180\')">'
}
由於我需要為所有文本框設置單獨的 id,因此我嘗試將變量 j 與它連接起來。 但它不起作用並將錯誤顯示為'Unknown identifier "startDate"+j"'
。請提出解決此問題的建議。解決此問題的任何其他想法也可以。順便說一句,我正在嘗試在 an.aspx 中實現它頁。
提前致謝。
誰能給我任何其他的例子來實現同樣的???? 請。
在將變量附加到字符串之前,您必須關閉字符串,以便將其識別為變量,而不僅僅是字母“j”。
所以。 . .
var j = 10,
str = "He is j years old";
. . . 行不通; 你需要這樣做:
var j = 10,
str = "He is " + j + " years old";
您的代碼應如下所示:
my_proj.innerHTML = my_proj.innerHTML +'<input type="text" namea="txtStartDate" id="txtStartDate' + j +'" runat="server" BackColor="#D6E4ED" BorderStyle="Inset" Width="10px" ></input>'
+'<a href="javascript:;" onclick="window.open(\'PopUp.aspx?textbox=txtStartDate' + j + '\',\'cal\',\'width=250,height=225,left=270,top=180\')">'
您需要重新格式化您的字符串,使其具有正確的特殊字符表達式。 雙引號和單引號需要有一個反斜杠,以便從字面上理解字符。 還有大量其他Javascript特殊字符: http://www.w3schools.com/js/js_special_characters.asp
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.