jquery post提交表單用法詳解

來源:互聯網
上載者:User

jquery $.post方法

在jquery中有這樣一個方法,$.post()下面就這個方法做一個簡單的執行個體:

jQuery.post( url, [data], [callback], [type] ) :
使用POST方式來進行非同步請求


參數:

url (String) : 發送請求的URL地址.

data (Map) : (可選) 要發送給伺服器的資料,以 Key/value 的索引值對形式表示。

callback (Function) : (可選) 載入成功時回呼函數(只有當Response的返回狀態是success才是調用該方法)。

type (String) : (可選)官方的說明是:Type of data to be sent。其實應該為用戶端請求的類型(JSON,XML,等等)


例


使用POST請求Ajax的代碼如下:

 代碼如下 複製代碼
$.post("student/Upload-addQuestion.action", {
 questionTypeName:questionTypeName,
 questionsTitle:questionsTitle,
 chapterId:chapterId,
 questionContent:questionContent,
 answerContent:answerContent,
 answerExplain:answerExplain
},function(data,textStatus){
 alert(data);
 $('.question_summary').val("");
 $('.quesion_textarea').val("");
 $('#question_answer').val();
 $('#question_answer_tips').val();
});


這裡的questionTypeName是變數名,也可以直接在html中擷取,

如:

questionTypeName:$('#question_type').val();

jquery ajax 設定type=post方法

$.ajax的一般格式

 代碼如下 複製代碼

$.ajax({

    type: 'POST',

    url: url ,

   data: data ,

   success: success ,

   dataType: dataType

});


二、$.ajax的參數描述

 
url 必需。規定把請求發送到哪個 URL。
data 可選。映射或字串值。規定連同請求發送到伺服器的資料。
success(data, textStatus, jqXHR) 可選。請求成功時執行的回呼函數。
dataType 可選。規定預期的伺服器響應的資料類型。

預設執行智能判斷(xml、json、script 或 html)。
 

參數描述


三、$.ajax需要注意的一些地方:

 1.data主要方式有三種,html拼接的,json數組,form表單經serialize()序列化的;通過dataType指定,不指定智能判斷。

 2.$.ajax只提交form以文本方式,如果非同步提交包含<file>上傳是傳過不過去,需要使用jquery.form.js的$.ajaxSubmit


_baidu_page_break_tag_

 代碼如下 複製代碼

<script type="text/javascript" src="/include/jquery/jquery-1.1.3.1.pack.js"></script>
<form name="form1" id="form1" action="ajax.aspx?action=1" method="post">
   <input type="hidden" id="comid" name="comid" value="111">
   <input type="hidden" id="jobid" name="jobid" value="222">
   <input type="hidden" id="userid" name="userid" value="333">
   <input type="submit" /></form>
<script language="javascript">
$("#form1").submit
    (
       function()
        {
        login();
        return false;
        }
    );
function login()
{
var comid = $("#comid").val();
var jobid = $("#jobid").val();
var userid = $("#userid").val();
$.ajax({
    type: "POST",
    url: "../include/ajax.php",
    data: "action=1" + "&comid=" + comid + "&jobid=" + jobid + "&userid=" + userid,
    beforeSend: function(){
    },
    success: function(msg){
  if(msg=="success"){
  alert("提交成功!")
   }else {
       alert("請不要重複提交!");
    }
    }
});
}
</script>

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.