Jquery serialize()方法不起作用

Bar*_*ğlu 1 jquery serialization

我有一个基本的HTML和JavaScript函数如下.当我按表单ID序列化表单时,它可以工作.但是我想要序列化一些不像这样的形式的html内容$('.FormReference').serialize().

我不能做什么?

    $(function () {

        $('#btnLogin').click(function (event) {

            alert($('#form1').serialize());
        });

    });


<form id="form1" runat="server" action="">
    <div>
        <table class="FormReference">
            <tr>
                <td>
                    <input type="text" id="_Username" name="Username" />
                </td>
                <td>
                    <input type="text" id="_Password" name="Password" />
                </td>
            </tr>
            <tr>
                <td colspan="2">
                    <input type="button" id="btnLogin" value="Login" />
                </td>
            </tr>
        </table>
    </div>
    </form>
Run Code Online (Sandbox Code Playgroud)

lon*_*day 6

文档:

.serialize()方法可以充当已选定的个别形式的元件,诸如jQuery对象上<input>,<textarea><select>.但是,通常更容易选择<form>标记本身进行序列化:

所以你有两个选择:

$('.FormReference').closest('form').serialize(); // serialize the whole form
$('.FormReference').find('input, select, textarea, button').serialize(); // serialize only elements contained within .FormReference
$('.FormReference').find(':input').serialize(); // shorter, but less efficient, alternative
Run Code Online (Sandbox Code Playgroud)

  • 那么`$('.FormReference').find(':input')`:-P (3认同)