jQuery 异步 提交表单

版权声明:借鉴时注明出处就行 https://blog.csdn.net/weixin_42144379/article/details/84899070

这个以前学过,但是后来工作用多了 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>

猜你喜欢

转载自blog.csdn.net/weixin_42144379/article/details/84899070