一、submit提交
一般表單提交通過type=submit實現,input type="submit",瀏覽器顯示為button按鈕,通過點擊這個按鈕提交表單數據跳轉到/url.do
<form action='http://www.123.com/postValue' method='post'> <input type='text' name='username'/> <input type='password' name='password'/> <input type='submit' value='登錄'/> </form>
當點擊登錄時,提交的數據是:
username=username&password=password
這種默認的提交方式,一般會進行頁面的跳轉(不成功時跳轉到當前頁面),而有時我們是對彈出框進行數據提交,希望提交成功則關閉彈出框並刷選父頁面,失敗則提示失敗原因,且彈出框不關閉。此時可采用ajax進行數據提交。
二、ajax提交form表單
$('#documentForm').submitForm({ url:"/Document/SubmitDocumentCreate", dataType:"text", callback:function(data){ endFileUpload(); data=eval("("+data+")"); alert(data.Content); if(data.Result > 0){ location.href = data.Redirect; } }, before:function(){ startFileUpload(); var errMsg =""; } }).submit();
此時可以在callback函數中對請求結果進行判斷,然后執行不同的動作(頁面跳轉或數據刷新、提醒錯誤)
三、form表單提交附件
需要設定form的enctype=“multipart/form-data”,並且添加<input type='file'>
而且附件只能通過submit方法進行提交
通過type=submit提交
一般表單提交通過type=submit實現,input type="submit",瀏覽器顯示為button按鈕,通過點擊這個按鈕提交表單數據跳轉到/url.do
<input type="text" name="name"/> <input type="submit" value="提交"> </form>
js提交form表單
js事件觸發表單提交,通過button、鏈接等觸發事件,js調用submit()方法提交表單數據,jquery通過submit()方法
<form id="form" action="/url.do" method="post"> <input type="text" name="name"/> </form> js: document.getElementById("form").submit(); jquery: $("#form").submit();