gpt4 book ai didi

Jquery Serialize() 方法不起作用

转载 作者:行者123 更新时间:2023-12-01 06:38:01 24 4
gpt4 key购买 nike

我有一个基本的 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>

最佳答案

来自the documentation :

The .serialize() method can act on a jQuery object that has selected individual form elements, such as <input>, <textarea>, and <select>. However, it is typically easier to select the <form> tag itself for serialization:

所以你有两个选择:

$('.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

关于Jquery Serialize() 方法不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9473436/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com