简体   繁体   中英

Append HTML FORM in JQuery or Javascript

I'm trying to append some HTML FORM codes but it seems that there's wrong in my code. When I try to click the button "Add another experience", it will go to a required field then say "Please fill in this field". What seems to be the problem? Here is my HTML CODE:

<div class="form-group row" style="width: 100%;">
                                <div class="col-md-12 exp">
                                    <label for="exp"><br>Do you have call center experience? &nbsp;&nbsp;&nbsp;</label>
                                    <label class="radio-inline"><input type="radio" name="exp" id="exp" value="Yes">Yes</label>
                                    <label class="radio-inline"><input type="radio" name="exp" id="exp" value="No">No</label>
                                    <br>
                                    <label for="exp"><br>Did you have any language training? &nbsp;&nbsp;&nbsp;</label>
                                    <label class="radio-inline"><input type="radio" name="training" id="training" value="Yes">Yes</label>
                                    <label class="radio-inline"><input type="radio" name="training" id="training" value="No">No</label>
                                    <br>
                                    <label for="exp"><br><br>Company Name: &nbsp;&nbsp;&nbsp;</label>
                                    <input type="input" style="width: 60%;" class="form-control" id="company_name" placeholder="Company Name">
                                    <br>
                                    <label for="exp"><br><br>Position: &nbsp;&nbsp;&nbsp;</label>
                                    <input type="input" class="form-control" id="position" placeholder="Position">
                                    <label for="bday">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Duration: &nbsp;&nbsp;&nbsp;</label>
                                    <input id="date-picker-2" type="date" class="date-picker form-control" placeholder="Date Started"/>
                                    <input id="date-picker-3" type="date" class="date-picker form-control" placeholder="Date Ended"/>&nbsp;<input type="checkbox" id="present" value="present">&nbsp;Present
                                    <br><br><br>
                                    <button class="btn btn-primary add_exp1" >Add another experience</button>
                                </div>
                            </div

And here is my JQUERY CODE:

 $(".add_exp1").click(function(){
           $(".exp").append('<br>
                                    <label for="exp">Another Work Experience<br><br>Company Name: &nbsp;&nbsp;&nbsp;</label>
                                    <input type="input" style="width: 60%;" class="form-control" id="company_name" placeholder="Company Name">
                                    <br>
                                    <label for="exp"><br><br>Position: &nbsp;&nbsp;&nbsp;</label>
                                    <input type="input" class="form-control" id="position" placeholder="Position">
                                    <label for="bday">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Duration:
 &nbsp;&nbsp;&nbsp;</label>
                                    <input id="date-picker-2" type="date" class="date-picker form-control" placeholder="Date Started"/>
                                    <input id="date-picker-3" type="date" class="date-picker form-control" placeholder="Date Ended"/>&nbsp;<input type="checkbox"
 id="present" value="present">&nbsp;Present
                                    <br><br><br>');         });

I just have it working like this.

Jquery in the head

<script src="http://code.jquery.com/jquery-2.1.3.min.js"></script>
        <script>
            $(document).ready(function () {
                $(".add_exp1").click(function(){
                    $(".exp").append('<br><label for="exp">Another Work Experience<br><br>Company Name: &nbsp;&nbsp;&nbsp;</label>' +
                        '<input type="input" style="width: 60%;" class="form-control" id="company_name" placeholder="Company Name">'+
                        '<br>'+
                        '<label for="exp"><br><br>Position: &nbsp;&nbsp;&nbsp;</label>'+
                        '<input type="input" class="form-control" id="position" placeholder="Position">'+
                        '<label for="bday">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Duration:'+
                        '&nbsp;&nbsp;&nbsp;</label>'+
                        '<input id="date-picker-2" type="date" class="date-picker form-control" placeholder="Date Started"/>'+
                        '<input id="date-picker-3" type="date" class="date-picker form-control" placeholder="Date Ended"/>&nbsp;<input type="checkbox"'+
                        'id="present" value="present">&nbsp;Present'+
                        '<br><br><br>');      

                });

            })


        </script>

And body in the html is the same as yours. The only change is the appending of the strings for the new elements in html. To have multiple lines, you need to escape it or append the strings.

When you add multiline string in javascript code you can escape the literal newline by '\\' otherwise newline symbol will break your js code. That's the right way to use multiline string:

$(".add_exp1").click(function(){
           $(".exp").append('<br>\
                            <label for="exp">Another Work Experience<br><br>Company Name: &nbsp;&nbsp;&nbsp;</label>\
                                    <input type="input" style="width: 60%;" class="form-control" id="company_name" placeholder="Company Name">\
                                    <br>\
                                    <label for="exp"><br><br>Position: &nbsp;&nbsp;&nbsp;</label>\
                                    <input type="input" class="form-control" id="position" placeholder="Position">\
                                    <label for="bday">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Duration:\
 &nbsp;&nbsp;&nbsp;</label>\
                                    <input id="date-picker-2" type="date" class="date-picker form-control" placeholder="Date Started"/>\
                                    <input id="date-picker-3" type="date" class="date-picker form-control" placeholder="Date Ended"/>&nbsp;<input type="checkbox"\
 id="present" value="present">&nbsp;Present\
                                    <br><br><br>');         });

Here is working fiddle

You have two ways to do that the most simple way is to wrap a part of html that you want to clone in a div. In your html:

<div class="form-group row" style="width: 100%;">
                                <div class="col-md-12 exp">
                                    <label for="exp"><br>Do you have call center experience? &nbsp;&nbsp;&nbsp;</label>
                                    <label class="radio-inline"><input type="radio" name="exp" id="exp" value="Yes">Yes</label>
                                    <label class="radio-inline"><input type="radio" name="exp" id="exp" value="No">No</label>
                                    <br>
                                    <label for="exp"><br>Did you have any language training? &nbsp;&nbsp;&nbsp;</label>
                                    <label class="radio-inline"><input type="radio" name="training" id="training" value="Yes">Yes</label>
                                    <label class="radio-inline"><input type="radio" name="training" id="training" value="No">No</label>
                                    <br>
                                    <div id ="partialForm">
                                    <label for="exp"><br><br>Company Name: &nbsp;&nbsp;&nbsp;</label>
                                    <input type="input" style="width: 60%;" class="form-control" id="company_name" placeholder="Company Name">
                                    <br>
                                    <label for="exp"><br><br>Position: &nbsp;&nbsp;&nbsp;</label>
                                    <input type="input" class="form-control" id="position" placeholder="Position">
                                    <label for="bday">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Duration: &nbsp;&nbsp;&nbsp;</label>
                                    <input id="date-picker-2" type="date" class="date-picker form-control" placeholder="Date Started"/>
                                    <input id="date-picker-3" type="date" class="date-picker form-control" placeholder="Date Ended"/>&nbsp;<input type="checkbox" id="present" value="present">&nbsp;Present
                                    </div>  
                                    <br><br><br>
                                    <button class="btn btn-primary add_exp1" >Add another experience</button>
                                </div>
                            </div>

I wrapped a part of the form in a div with id = "partialForm" Then I clonned this part in jquery and added specific html elements

$(".add_exp1").click(function(){

    var newForm= $("#partialForm").clone();
    $('input',newForm).val('');
    var generatedBr = $(document.createElement('br'));
    var header = $(document.createElement('label'))
                      .attr('for','exp')
                      .text('Another Work Experience');
    $('.exp').append( generatedBr,
                     header,
                     newForm);
});

Here is the working fiddle: http://jsfiddle.net/defee/nna15kph/ Another way is to generate Html from javascript with the use of best practices.

Here there is an example how to generate an input in jquery:

var dateStartInput = $(document.createElement('input'))
          .attr('id','date-picker-2')
          .attr('type','date')
          .attr('placeholder','Date Started')
          .addClass('date-picker form-control');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM