这就是你会使用AJAX的。这些答案假定你在同一个域上工作。
最简单的办法
使用AJAX是使用jQuery最简单的方法,但是这不是绝对必要的(见下文)。 http://www.queness.com/post/160/create-a-ajax-based-form-submission-with-jquery
通用示例:
$.ajax({
url: "test.html",
context: document.body
}).done(function() {
$(this).addClass("done");
});
http://api.jquery.com/jQuery.ajax/
的不容易的方式
如果你不想使用jQuery,你也离不开它。 http://www.webreference.com/programming/javascript/ajax_forms/index.html
样本HTML
<p><html><br/>
<head><br/>
<title>Form Posts with Ajax</title><br/>
<script type="text/javascript" src="js/Ajax.js"></script><br/>
<script type="text/javascript" src="js/Post.js"></script><br/>
</head><br/><br/>
<body><br/><br/>
<form action="bridge.php" method="post" onsubmit="Post.Send(this); return false;"><br/>
Name:<br/><input type="text" name="name" /><br/>
<br/><br/><br/>
Message:<br/><textarea name="message"></textarea><br/>
<br/><input type="submit" value="submit" /><br/>
</form><br/><br/>
</body><br/>
</html></p>
样品的JavaScript
var Ajax = new Object();
Ajax.isUpdating = true;
Ajax.Request = function(method, url, query, callback)
{
this.isUpdating = true;
this.callbackMethod = callback;
this.request = (window.XMLHttpRequest)? new XMLHttpRequest(): new ActiveXObject("MSXML2.XMLHTTP");
this.request.onreadystatechange = function() { Ajax.checkReadyState(); };
if(method.toLowerCase() == 'get') url = url+"?"+query;
this.request.open(method, url, true);
this.request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
this.request.send(query);
}
Ajax.checkReadyState = function(_id)
{
switch(this.request.readyState)
{
case 1: break;
case 2: break;
case 3: break;
case 4:
this.isUpdating = false;
this.callbackMethod(this.request.responseXML.documentElement);
}
}
加载用于AJAX的jQuery看起来像是一种矫枉过正。 – Francisc 2012-04-08 01:17:52
所以你说我需要重做我的整个表单?有没有简单的方法呢? – user755889 2012-04-08 01:22:22
@ user755889不,你的表格不应该被改变。您只需添加一些JavaScript来拦截提交并使用AJAX发送。请参阅我发布的链接。 – msigman 2012-04-08 01:24:13