[英]Dynamically created textboxes don't work with $_POST
對於投票系統,人們看到3個文本框。 但是他們可以手動添加文本框,以防他們想投票給更多藝術家。 但是這些文本框是使用以下代碼通過Javascript動態創建的:
$(document).ready(function(){
var counter = 4;
$("#addButton").click(function () {
var newTextBoxDiv = $(document.createElement('div'))
.attr("id", 'TextBoxDiv' + counter);
newTextBoxDiv.after().html('<label>Textbox #'+ counter + ' : </label>' +
'<input type="text" name="textbox[]" id="textbox' + counter + '" value="" style="width:630px; height:30px; font-size:18px; opacity:0.9;" runat=server>');
newTextBoxDiv.appendTo("#TextBoxesGroup");
counter++;
if(counter==11){
document.getElementById('addButton').style.visibility='hidden';
document.getElementById('resetButton').style.position="absolute";
document.getElementById('resetButton').style.left="60px";
return false;
}
});
$("#removeButton").click(function () {
if(counter==1){
alert("No more textbox to remove");
return false;
}
counter--;
$("#TextBoxDiv" + counter).remove();
});
$("#getButtonValue").click(function () {
var msg = '';
for(i=1; i<counter; i++){
msg += "\n Textbox #" + i + " : " + $('#textbox' + i).val();
}
alert(msg);
});
});
這些文本框在其中生成的表單將重定向到“ confirm.php”,但在confirm.php中,我無法從動態創建的文本框獲取值。
如何解決這個問題?
也許您可以嘗試將代碼更改為以下代碼,因為我想知道JQuery是否有問題:
順便說一句, newTextBoxDiv.after().html
的after()
方法完全沒有用。
希望這會有所幫助。
文本框將需要定義“名稱”屬性。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.