Home  >  Article  >  Web Front-end  >  jQuery plug-in jQuery.Form.js usage example analysis (with demo sample source code)_jquery

jQuery plug-in jQuery.Form.js usage example analysis (with demo sample source code)_jquery

WBOY
WBOYOriginal
2016-05-16 15:22:061253browse

The example in this article describes the usage of jQuery.Form.js of jQuery plug-in. Share it with everyone for your reference, the details are as follows:

1. The jQuery.Form.js plug-in is used to implement Ajax submission forms.

Method:

1.formSerilize() is used to serialize the data in the form and automatically organize it into a URL address format suitable for AJAX asynchronous requests.

2.clearForm() Clears the contents of all input values ​​in the form.

3.restForm Reset all field contents in the form. That is, restore all fields in the form to their default values ​​when the page loads.

Question: The difference between ajaxForm() and ajaxSubmit():

Answer: $("#form1").ajaxForm(); is equivalent to the following two lines:

$("#form1".submit)(function(){
 $("#form1").ajaxSubmit();
return false;
})

That is to say, ajaxFrom() will automatically prevent form submission. AjaxSubmit() will not automatically prevent form submission. If you want to prevent form submission, you must return false;

Tip 1: If you want to avoid jumping after the form submission is completed, then a simple line of code can achieve it, which is almost the same as not using form submission:

$("#MailForm").ajaxSubmit(function(message) {
  alert("表单提交已成功!");
});

Note:Both ajaxForm() and ajaxForm() can have no parameters or accept 1 parameter. This parameter can be either a callback function or an options object. This object is very powerful and is described as follows:

var options={
 url:url, //form提交数据的地址
 type:type, //form提交的方式(method:post/get)
 target:target, //服务器返回的响应数据显示在元素(Id)号确定
 beforeSubmit:function(), //提交前执行的回调函数
 success:function(), //提交成功后执行的回调函数
 dataType:null, //服务器返回数据类型
 clearForm:true, //提交成功后是否清空表单中的字段值
 restForm:true, //提交成功后是否重置表单中的字段值,即恢复到页面加载时的状态
 timeout:6000 //设置请求时间,超过该时间后,自动退出请求,单位(毫秒)。
}

Example:

Page js code:

<script src="jQuery.1.8.3.js" type="text/javascript"></script>
<script src="jQuery.Form.js" type="text/javascript"></script>
<script type="text/javascript">
$(function () {
  $(":submit").click(function () {
    var options = {
      url: "indexAjax.aspx",
      target: "#div2",
      success: function () {
        alert("ajax请求成功");
      }
    };
    $("#form1").ajaxForm(options);
  })
})
</script>

Page HTML code:

<div id="div1">
<form id="form1" method="get" action="#">
  <p>我的名字:<input type="text" name="name" value="请输入内容" /></p>
  <p>我的偶像是:<input type="radio" name="Idol" value="刘德华" />刘德华  <input type="radio" name="Idol" value="张学友" />张学友</p>
  <p>我喜欢的音乐类型:<input type="checkbox" name="musictype" value="1.摇滚">摇滚 <input type="checkbox" name="musictype" value="2.轻松">轻柔 <input type="checkbox" name="musictype" value="3.爵士">爵士</p>
  <p><input type="submit" value="确认" /></p>
</form>
</div>
<div id="div2">
</div>

Backend: indexAjax.aspx.cs code

protected void Page_Load(object sender, EventArgs e)
{
  string strName = Request["name"];
  string strIdol = Request["Idol"];
  string strMusicType = Request["musictype"];
  Response.Clear();
  Response.Write("我的名字是:" + strName + ";  我的偶像是:" + strIdol + ";  我喜欢的音乐类型:" + strMusicType);
  Response.End();
}

Click here for sample codeDownload from this site.

For more jQuery plug-in usage, you can also refer to the relevant topics on this site : "Summary of jQuery common plug-ins and usage".

I hope this article will be helpful to everyone in jQuery programming.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn