Javascript 如何阻止窗体自动重置?
有谁能告诉我,在返回到表单并重置之前,如何阻止表单页面仅在几分之一秒内显示此jquery脚本的结果 我打算让表单向下滚动到显示结果的锚点Javascript 如何阻止窗体自动重置?,javascript,jquery,Javascript,Jquery,有谁能告诉我,在返回到表单并重置之前,如何阻止表单页面仅在几分之一秒内显示此jquery脚本的结果 我打算让表单向下滚动到显示结果的锚点#results。这是可行的,但是结果只在一瞬间显示,页面会快速返回到表单a重置 我确信我应该使用returnfalse或/和e.preventDefault()
#results
。这是可行的,但是结果只在一瞬间显示,页面会快速返回到表单a重置
我确信我应该使用returnfalse代码>或/和e.preventDefault()但是我不确定他们应该去哪里。非常感谢
$(document).ready(function() {
$("#message").hide();
$("#myform").validate({
submitHandler: function() {
$('#gauge').empty();
var formdata = $("#myform").serialize();
//Post form data
$.post('php_includes/insert.php', formdata, function(data) {
//Process post response
});
//Reset Form
$('#myform')[0].reset();
fetchRowCount();
}
});
//Fetch data from server
function fetchRowCount() {
$.ajax({
url: 'php_includes/server.php',
dataType: "json",
success: function(data) {
$("#rows").html(data.rows);
$("#min").html(data.min);
$("#max").html(data.max);
$("#mean").html(data.total);
$("#last").html(data.last_entry);
//Show gage once json is receved from server
var gage = new JustGage({
id: "gauge",
value: data.total,
min: data.min,
max: data.max,
title: "Sample Data"
});
}
});
//Scroll to Results
$('html, body').animate({
scrollTop: $('#results').offset().top
}, 'slow');
$("#gauge").fadeIn(slow);
}
});
也许您只需要将滚动条移动到成功承诺:
//Fetch data from server
function fetchRowCount() {
$.ajax({
url: 'php_includes/server.php',
dataType: "json",
success: function(data) {
$("#rows").html(data.rows);
$("#min").html(data.min);
$("#max").html(data.max);
$("#mean").html(data.total);
$("#last").html(data.last_entry);
//Show gage once json is receved from server
var gage = new JustGage({
id: "gauge",
value: data.total,
min: data.min,
max: data.max,
title: "Sample Data"
});
//Scroll to Results
$('html, body').animate({
scrollTop: $('#results').offset().top
}, 'slow');
$("#gauge").fadeIn(slow);
}
});
}
//Fetch data from server
function fetchRowCount() {
$.ajax({
url: 'php_includes/server.php',
dataType: "json",
success: function(data) {
$("#rows").html(data.rows);
$("#min").html(data.min);
$("#max").html(data.max);
$("#mean").html(data.total);
$("#last").html(data.last_entry);
//Show gage once json is receved from server
var gage = new JustGage({
id: "gauge",
value: data.total,
min: data.min,
max: data.max,
title: "Sample Data"
});
//Scroll to Results
$('html, body').animate({
scrollTop: $('#results').offset().top
}, 'slow');
$("#gauge").fadeIn(slow);
}
});
}
实际上,您正在尝试在表单数据成功提交之前从服务器获取数据
尝试将post之后应执行的内容放入其回调中:
//Post form data
$.post('php_includes/insert.php', formdata, function(data) {
//Process post response
//Reset Form
$('#myform')[0].reset();
fetchRowCount();
});
向下滚动时也是如此。您应该在成功从服务器获取数据后滚动