Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
758 views
in Technique[技术] by (71.8m points)

jquery: Ajax: how to prepend html string recieved with new line characters

I have url which gives html string as repsonse

Examples: the following is the html

<div class="form-group">
    <label for="id_password">
        Password
    </label>
    <input class="form-control" id="id_password" maxlength="128" name="password" placeholder="Password" required="" title="" type="text"/>
</div>
<div class="form-group">
    <label for="id_last_login">
        Last login
    </label>
    <input class="form-control" id="id_last_login" name="last_login" placeholder="Last login" title="" type="text"/>
</div>
<div class="form-group">
    <div class="form-check">
        <input class="form-check-input" id="id_is_superuser" name="is_superuser" type="checkbox">
            <label class="form-check-label" for="id_is_superuser" title="Designates that this user has all permissions without explicitly assigning them.">
                Superuser status
            </label>
            <small class="form-text text-muted">
                Designates that this user has all permissions without explicitly assigning them.
            </small>
        </input>
    </div>
</div>

it sent as a string like with

<div class="form-group"><label for="id_password">Password</label><input type="text" name="password" maxlength="128" class="form-control" placeholder="Password" title="" required id="id_password"></div>
<div class="form-group"><label for="id_last_login">Last login</label><input type="text" name="last_login" class="form-control" placeholder="Last login" title="" id="id_last_login"></div>
<div class="form-group"><div class="form-check"><input type="checkbox" name="is_superuser" class="form-check-input" id="id_is_superuser"><label class="form-check-label" for="id_is_superuser" title="Designates that this user has all permissions without explicitly assigning them.">Superuser status</label>
    <small class="form-text text-muted">Designates that this user has all permissions without explicitly assigning them.</small>

</div></div>

I have the following form i which i have to insert this html code recieved

    <form action="/url/to/submit/" method="post" class="form" id="id_form">
        <!-- Here i have to insert he html i get from ajax -->
        <button type="submit" class="btn btn-primary">
          Submit
        </button>
    </form>

I am trying the following:

        $.ajax({
            url: "/get_form_fields",
            type: 'GET',
            success: function(res) {
                $("#id_form").prepend(res)
            },
            error: function(err) {
                console.log(err)
            }
        })

What i see is the form element is removed and it adds a <pre>html code of the fields</pre>

How can i do this


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...