jQuery表单插件还是jQuery序列化? [英] Jquery Form Plugin or Jquery serialization?

查看:110
本文介绍了jQuery表单插件还是jQuery序列化?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道 jQuery表单插件序列化?

如果我选择使用表单插件,可以使用jQuery 1.5的新功能吗?

If I do choose to go with the form plugin can I use the new jQuery 1.5 features?

表单插件还可以阻止多个回发吗?我通常使用 Ajax管理器插件来停止重复的ajax帖子,但我认为我不能将其与表单插件.

Also does the Form Plugin have anything to stop multiple post backs? I usually use the Ajax manager plugin to stop duplicate ajax posts but I don't think I can use it with Form Plugin.

谢谢

推荐答案

jQuery表单插件提供了将Ajax与表单结合使用的简便性.它将使用表单属性来确定如何以及在何处提交表单.表单的method属性告诉插件要使用的请求类型.表单的action属性告诉它在哪里提交表单.

The jQuery Form Plugin offers simplicity of using ajax with forms. It will use the form attributes to determine how and where to submit the form. The form's method attribute tells the plugin the request type to use. The form's action attribute tells it where to submit the form.

考虑这样的示例表单:

<form id="myform" action="submit.php" method="post">
<!--inputs and submit button go here-->
</form>

从本质上讲,它允许您编写:

In essence it allows you to write:

$('#myform').ajaxForm();

代替

$.post("submit.php", $("#myform").serialize());

jQuery Form插件还将允许您从代码中通过Ajax提交.

The jquery Form plugin will also allow you to submit via Ajax from your code.

$('#myform').ajaxSubmit();

无论如何,jQuery Forms插件都会序列化表单,您必须先序列化才能提交到服务器. jQuery Form Plugin只是在后台序列化表单.上面的示例不处理服务器的任何响应.
使用下面的代码,您可以将响应附加到其类属性包含结果"的元素.

The jQuery Forms Plugin will serialize the form anyway, your going to have to serialize before you submit to the server. The jQuery Form Plugin just serializes the form behind the scenes. The examples above do not handle any responses from the server.
Using the code below you can append the response to elements whose class attribute contains "result".

$('#myform').ajaxForm({ target : ".result"});

您可以使用jQuery支持的任何选择器作为目标选项.

You can use any selector that is supported in jQuery for the target option.

我不确定是否可以使用延迟方法.我对此表示怀疑,因为兼容性是针对1.3.2+的,而本机ajax方法已包装在插件中.这样,就不会从插件内部返回Deferred对象.

I'm not sure if you can use deferred methods with it or not. I doubt it because the compatibility is for 1.3.2+ and the native ajax methods are wrapped up in the plugin. Such that the Deferred object is never returned from the plugin internals.

jQuery表单确实具有可以使用的beforeSubmit选项.因此,在上面的代码中添加以下内容:

The jQuery form does have a beforeSubmit option that can be used. So adding to the above code we get:

$('#myform').ajaxForm({ target: ".result", beforeSubmit: function(arr, $form, options) {
        // arr: The array of form data
        // The array of form data takes the following form: 
        // [ { name: 'username', value: 'jresig' }, { name: 'password', value: 'secret' } ] 

        //some code to check if we already submitted the form

        // return false to cancel submit                  
    }
});

但是,如果用户在提交表单后完成了页面操作,则建议不要这样做.这只会给您的客户端代码增加不必要的复杂性.如果用户在提交表单后在页面上做其他事情,应该没问题. jQuery Forms插件有一个成功选项,如果服务器返回200"OK"响应,则该函数将调用函数回调.您也可以在作者网站上查看插件 http://jquery.malsup.com/form/有关该插件的更多信息.

However, I would recommend against doing that if user is done with the page after they submit the form. It would only add unneeded complexity to your client-side code. Should be okay if the user will be doing other stuff on the page after the form submission. The jQuery Forms Plugin has a success option that takes a function callback to be called if the server returns a 200 "OK" response. You can also check out the authors website for the plugin http://jquery.malsup.com/form/ for much more info on the plugin.

这篇关于jQuery表单插件还是jQuery序列化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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