2016-12-06 64 views
0

我已經看過這個簡單的腳本的所有版本,我無法得到它的工作。JS:添加文本輸入動態工作不

按鈕添加字段,我檢查源代碼,我按一下按鈕,它增加了使用正確的名稱正確的字段=但是當我提交,沒有信息從動態添加領域轉移。

var count = 2; 
 
function addLine() { 
 
    var table = document.getElementById("myTable"); 
 
    var row = table.insertRow(-1); 
 
    var cell1 = row.insertCell(0); 
 
    var cell2 = row.insertCell(1); 
 
\t var cell3 = row.insertCell(2); 
 
\t var cell4 = row.insertCell(3); 
 
\t var cell5 = row.insertCell(4); 
 
\t cell1.setAttribute("class", "odd"); 
 
\t cell2.setAttribute("class", "odd"); 
 
\t cell3.setAttribute("class", "odd"); 
 
\t cell4.setAttribute("class", "odd"); 
 
\t cell5.setAttribute("class", "odd"); 
 
\t var input1 = document.createElement('INPUT'); 
 
\t input1.setAttribute("type", "text"); 
 
\t input1.setAttribute("name", "input" + count + "d1"); 
 
\t input1.setAttribute("class", "whtext"); 
 
\t cell1.appendChild(input1); 
 
\t cell2.innerHTML = "<input class='whtext' name='input" + count + "d2' type='text'>"; 
 
\t cell3.innerHTML = "<input class='whtext' name='input" + count + "d3' type='text'>"; 
 
\t cell4.innerHTML = "<input class='whtext' name='input" + count + "d4' type='text'>"; 
 
\t cell5.innerHTML = "<input class='whtext' name='input" + count + "d5' type='text'>"; 
 
\t document.getElementById("counter").value = count; 
 
\t count++; 
 
}

正如你所看到的,我有兩個不同的版本,我已經試過,用的appendChild,只是添加到innerHTML的。我是JS的新人,所以這讓我難住。

我已經運行的var_dump($ _ POST);並沒有任何JS添加字段顯示。

+0

您可以先做的最好的事情是將它分成諸如createCell(),createInput()之類的函數。這樣你的代碼就會更有條理,更容易改變,這不是最好的方式,我可以爲你的問題提供一個答案,但我不會真正幫助你。 – ocespedes

+0

我懷疑input1.setAttribute(「名」,...)不工作的香港專業教育學院通過使用setAttribute看到情況下,瀏覽器不會讓你設置的名稱..具有u嘗試,而不是DOM方法作爲測試的innerHTML? 也是什麼瀏覽器? –

+0

我們需要HTML代碼! –

回答

0

假設這HTML:

<form> 
    <input type="hidden" id="counter"> 
    <table id="myTable"> 
    </table> 
    <input type="submit" value="submit"> 
</form> 

試試這個JS:

var count = 2; 

function addLine() { 
    var table = document.getElementById("myTable"); 
    var row = table.insertRow(-1); 
    var noOfCols = 4; 
    for (var i = 0; i < noOfCols; i++) { 
    var cell = row.insertCell(i); 
    cell.setAttribute("class", "odd"); 
    cell.innerHTML = "<input class='whtext' name='input" + count + "d" + (i + 1) + "' type='text'>"; 
    } 

    document.getElementById("counter").value = count; 
    count++; 
} 

addLine(); 

的jsfiddlehttps://jsfiddle.net/3tbekvc1/ (檢查網絡後提交給驗證形式瓦爾是得到發送)

+0

對不起,但腳本工作正常。我已經添加了我想要的字段,所有內容,並且添加了正確的名稱到添加的輸入字段,因此它們都是單獨命名的,但是當我進入提交的頁面時,輸入的數據不會顯示出來。 – MikeV

+0

也是你確定你的桌子被包裹在

? –

+0

是的,我有一個與此腳本在同一個表中的輸入字段,它們被編碼到HTML中,並且它們發送數據的速度很快。它的JavaScript添加輸入字段不。 – MikeV