抱歉,我对这个问题感到抓狂。每次我调用 jQuery 时,它都会重新加载我的页面。
HTML 文件:
<body>
<form enctype='multipart/form-data' id="form_id">
---- some inputs here ------
<input type='submit' value='Basic search'>
</form>
<script type="text/javascript">
$(document).ready(function() {
$(document).on('submit', '#form_id', function() {
var data = $(this).serialize();
$.ajax({
type : 'POST',
url : 'phpfile.php',
data : data,
success : function(data)
{$(".result").html(data);} //result is div that i want display result in it
});
return false;
});
});
</script>
</body>
编辑 1:
我用<input type ='button'>
安装了<input type = 'submit'>
但它不起作用。
我添加
$(document).on('click', '#search_button', function(e)
{
e.preventDefault();
但是,它不起作用!
编辑 2: 控制台中的错误消息: Form 包含 enctype=multipart/form-data,但不包含 method=post。使用 method=GET 正常提交,而不使用 enctype。
表单以 windows-1252 编码提交,该编码无法对所有 Unicode 字符进行编码,因此用户输入可能会损坏。为了避免此问题,应更改页面,以便通过将页面本身的编码更改为 UTF-8 或在表单元素上指定 Accept-charset=utf-8 来以 UTF-8 编码提交表单。
表单包含文件输入,但表单上缺少 method=POST 和 enctype=multipart/form-data。该文件将不会被发送。
改变对象的[[Prototype]]会导致你的代码运行非常慢;而是使用 Object.create 创建具有正确初始 [[Prototype]] 值的对象
未声明 HTML 文档的字符编码。如果文档包含 US-ASCII 范围之外的字符,则在某些浏览器配置中,该文档将呈现为乱码。页面的字符编码必须在文档或传输协议(protocol)中声明。
主线程上的同步 XMLHttpRequest 已被弃用,因为它会对最终用户的体验产生不利影响。如需更多帮助http://xhr.spec.whatwg.org/
Web 控制台日志记录 API(console.log、console.info、console.warn、console.error)已被此页面上的脚本禁用。
最佳答案
我通过用 jQuery 替换 $ 来解决问题
jQuery.ajax insted of $.ajax
关于javascript - 在提交事件上调用 jQuery 重新加载页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35586178/