在'form'中添加div不起作用

时间:2017-03-02 11:32:50

标签: javascript jquery html

我试图将'div'元素及其内容附加到另一个'div'中,但这不符合'form'标签,在其正常工作之外......

<form id="myForm">
            <div id ="wrapfields" class="input_fields_wrap">
            <div class="col-md-12 form-group">
                <button style ="margin-left:1%;"class="btn btn-success"onclick="Addelemt()">+</button>
            </div>
            <div>
                    <div class="col-md-6 form-group">
                      <label class="text-center">Add Items</label>
                      <input type="text" class="form-control" name="addItem[]" id="email">
                    </div>
                    <div class="col-md-2   form-group">
                      <label>Price</label>
                      <input type="text" class="form-control" name="addprice[]" id="email">
                   </div>
                   <div class="col-md-2   form-group">
                     <label>Quantity</label>
                     <input type="text" class="form-control" name="addprice[]" id="email">
                  </div>
            </div>
            <div id ="" style="margin-bottom:10px;">
            <div class="col-md-2 form-group" style="margin-left:45%;">
            <button  type ="submit" onsubmit ="return saveInputs()" name ="submit" class="btn btn-default"/>submit</button>   
            </div>
            </div>
        </div>
        </form>

和到目前为止写的javascript代码..

var counter = 0;var increment=0;

function Addelemt()
{   
var parentDiv = document.getElementById('wrapfields');  
    increment++;
    var divClass = document.createElement("div");
    divClass.setAttribute('id','divId'+increment);
    console.log(divClass);
    parentDiv.appendChild(divClass);

}

任何人都可以告诉我为什么它不能在'form'中工作???

1 个答案:

答案 0 :(得分:3)

所以,问题在于你的HTML

<button style ="margin-left:1%;" class="btn btn-success" onclick="Addelemt()">+</button>

type="button"添加到“添加”按钮

<button style ="margin-left:1%; "class="btn btn-success" type="button" onclick="Addelemt()">+</button>

由于button标记内的<form> type标记没有Submit属性,因此会将其视为{ "username":"tester@tester.com", "Course":[ { "Date":{ "DatePlayed":"Fri Jul 21 2017 00:00:00 GMT 0100 (GMT Daylight Time)", "Course":{ "CoursePlayed":"testerCourse", "holes":[ { "holeno":0, "score":"", "putts":"", "penalty":"", "bunker":"", "green":"", "fairway":"" } ] } } } ] } 按钮,因此会刷新页面。