jQuery 非同步 提交表單
阿新 • • 發佈:2018-12-11
這個以前學過,但是後來工作用多了 MXFramework 的 RESTClient 做互動 有些淡忘了
雖然 RESTClient跟 ajax 差不太多,但還有區別
今天覆習下 jQuery 的Ajax提交表單:
html 部分:
<form id="form-msg"> <!-- Leave me a message... --> <textarea id="aeraMsg" name="leave_msg"></textarea> <input name="user_name" type="text" value="Your name or nickname"> <input id="submitMsg" type=button value="Send" /> </form>
JavaScript 部分:
window.onload = function(){ //注意在此之前要 引入 jQuery $("#submitMsg").click( function () { $.ajax({ type:'post', data:$("#form-msg").serialize(), url:"/jacktu/main/send_msg.do", dataType:"text", success:function(data){ if(data=="successful"){ $("#aeraMsg").val("").focus(); alert("提交成功!"); }else{ alert("提交失敗!"); } },error:function(){ alert("錯誤!"); } }) } ); } </script>