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
378 views
in Technique[技术] by (71.8m points)

How to get form html() via jQuery including updated value attributes?

Is it possible to get the html of a form with updated value attributes via the .html() function?

The (simplified) HTML:

<form>
    <input type="radio" name="some_radio" value="1" checked="checked">
    <input type="radio" name="some_radio" value="2"><br><br>
    <input type="text" name="some_input" value="Default Value">
</form><br>
<a href="#">Click me</a>

The jQuery:

$(document).ready(function() 
{
    $('a').on('click', function() {
        alert($('form').html());
    });
});

Here is an example of what I am trying to do: http://jsfiddle.net/brLgC/2/

After changing the value of the input and pressing "click me" it still returns the HTML with the default values.

How can I simply get the updated HTML via jQuery?

See Question&Answers more detail:os

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

1 Reply

0 votes
by (71.8m points)

If you really must have the HTML, you need to actually update the "value" attribute manually: http://jsfiddle.net/brLgC/4/

$(document).ready(function() 
{
    $('a').on('click', function() {
        $("input,select,textarea").each(function() {
           if($(this).is("[type='checkbox']") || $(this).is("[type='checkbox']")) {
             $(this).attr("checked", $(this).attr("checked"));
           }
           else {
              $(this).attr("value", $(this).val()); 
           }
        });
        alert($('form').html());
    });
});

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

...