jquery load()仅适用于alert()
问题描述:
下面的代码仅在我取消注释警报时起作用。这段代码有什么问题。
temp.htmljquery load()仅适用于alert()
<script src="jquery.js" type="text/javascript"></script>
<div id="show"></div>
<form id="chat_form">
<input type="submit" />
</form>
<div id="abc"></div>
<script>
$(document).ready(function() {
$("#chat_form").submit(function (event) {
$("#show").load("test.html", function (responseTxt, statusTxt, xhr) {
if (statusTxt == "success")
$("#abc").text(responseTxt);
if (statusTxt == "error")
$("#abc").text(xhr.statusText);
//alert(xhr.statusText);
});
});
});
</script>
的test.html
<p>hi i am test.html</p>
答
您应该防止形式来得到提交:
$("#chat_form").submit(function (event) {
event.preventDefault(); // <-----add this to stop the form submission.
+0
然后警报如何解决问题,因为警报是在ajax回调..所以到时候ajax请求完成表格应该有submited ...除非它是一个同步阿贾克斯请求不是它 – 2015-03-03 08:07:20
+0
@ArunPJohny我猜想警报保持屏幕在那一点,OP是能够看到在该点的文本在该点的时间,但当注释出来的形式只是被提交,没有什么在div中。我想这是问题。 – Jai 2015-03-03 08:11:57
答
使用此
$("#chat_form").submit(function (event) {
event.stopPropagation();
});
尝试阻止默认表单提交操作 - http://jsfiddle.net/arunpjohny/upfjLurm/1/ – 2015-03-03 08:05:26