如何通过jQuery获取表单html(),包括更新的值属性? [英] How to get form html() via jQuery including updated value attributes?

查看:140
本文介绍了如何通过jQuery获取表单html(),包括更新的值属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可以通过.html()函数获取具有更新值属性的表单的HTML?

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

(简化的)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>

jQuery:

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

这是我正在尝试做的一个例子:
http://jsfiddle.net/brLgC/2/

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

在更改输入并按点击我,它仍然返回带有默认值的HTML。

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

如何通过jQuery获取更新的HTML?

How can I simply get the updated HTML via jQuery?

推荐答案

如果您确实需要HTML,则需要手动更新value属性:
http://jsfiddle.net/brLgC/4/

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());
    });
});

这篇关于如何通过jQuery获取表单html(),包括更新的值属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆