2016-05-30 2 views
0

Я хочу, чтобы динамически добавлять формы поля ввода формы с помощью JavaScript ниже, нажав на кнопку добавления:Добавление поля ввода в HTML-форме

var i = 0; 
function increment() { 
i += 1; 
} 

function addfieldFunction() { 
    var r = document.createElement('div'); 
    var y = document.createElement("INPUT"); 
    var z = document.createElement("INPUT"); 
    y.setAttribute("class", "dash-input"); 
    y.setAttribute("type", "text"); 
    y.setAttribute("placeholder", "University"); 
    z.setAttribute("class", "dash-input"); 
    z.setAttribute("type", "text"); 
    z.setAttribute("placeholder", "Course"); 
    increment(); 
    y.setAttribute("Name", "a_level[ + i ][0]"); 
    r.appendChild(y); 
    z.setAttribute("Name", "a_level[ + i ][1]"); 
    r.appendChild(z); 
    document.getElementById("form-div").appendChild(r); 
} 



<form class = "dash-form" method = "POST" action = "/" > 
    <div id = "form-div"> 
     <input class = "dash-input" type = "text" name = "a_level[+i][0]" value = "" placeholder = "University"/> 
     <input class = "dash-input" type = "text" name = "a_level[+i][1]" value = "" placeholder = "Course"/> 
    </div> 
    <div class = "dash-submit"> 
     <input class = "dash-submit-button" type = "submit" value = "Submit"/> 
    </div> 
</form> 
<div class = "dash-add"> 
    <button class = "dash-add-button" onclick = "addfieldFunction()">ADD</button> 
</div> 

Функция возвращает я вместо приращением и возвращая целое число, как показано ниже:

<div id = "form-div"> 
    <input class = "dash-input" type = "text" name = "a_level[0][0]" value = "" placeholder = "University"/> 
    <input class = "dash-input" type = "text" name = "a_level[0][1]" value = "" placeholder = "Course"/> 
</div> 

ответ

4

Объединить переменной i с помощью оператора +(concatenation operator)

concatenation operator (+) объединяет два строковых значения вместе, возвращая другую строку, которая является union двух операндов.

var i = 0; 
 

 
function increment() { 
 
    i += 1; 
 
} 
 

 
function addfieldFunction() { 
 
    var r = document.createElement('div'); 
 
    var y = document.createElement("INPUT"); 
 
    var z = document.createElement("INPUT"); 
 
    y.setAttribute("class", "dash-input"); 
 
    y.setAttribute("type", "text"); 
 
    y.setAttribute("placeholder", "University"); 
 
    z.setAttribute("class", "dash-input"); 
 
    z.setAttribute("type", "text"); 
 
    z.setAttribute("placeholder", "Course"); 
 
    increment(); 
 
    y.setAttribute("name", "a_level[ " + i + " ][0]"); //Keep attribute in lower case 
 
    r.appendChild(y); 
 
    z.setAttribute("name", "a_level[ " + i + "][1]"); 
 
    r.appendChild(z); 
 
    document.getElementById("form-div").appendChild(r); 
 
}
<form class="dash-form" method="POST" action="/"> 
 
    <div id="form-div"> 
 
    <input class="dash-input" type="text" name="a_level[+i][0]" value="" placeholder="University" /> 
 
    <input class="dash-input" type="text" name="a_level[+i][1]" value="" placeholder="Course" /> 
 
    </div> 
 
    <div class="dash-submit"> 
 
    <input class="dash-submit-button" type="submit" value="Submit" /> 
 
    </div> 
 
</form> 
 
<div class="dash-add"> 
 
    <button class="dash-add-button" onclick="addfieldFunction()">ADD</button> 
 
</div>

3

Вам нужно сцепить это следующим образом:

y.setAttribute("name", "a_level[" + i +"][0]"); 

Как Rayon отметил: сохранить атрибуты в нижнем регистре, хотя стандарт HTML5 не требует нижнего атрибута дела имена (см. here). W3C рекомендует его, хотя проверка XHTML завершится неудачно, используя прописные буквы.

Смежные вопросы