[英]How to prevent page refresh upon form.trigger('submit')?
I have the following code which works well when triggered by the user clicking on the form's submit button: 我有以下代码,当用户点击表单的提交按钮触发时,该代码效果很好:
// load dashboards when filter form is submitted
$('div.active form.filter-form').submit(function (e) {
// get submitted values
submittedData = $(this).serializeArray();
getDashboardURL();
e.preventDefault();
});
However whenever I try to trigger the form submission in the snippet below, it results in the page refreshing: 但是,每当我尝试在下面的代码段中触发表单提交时,都会导致页面刷新:
// trigger reload of dashboard
$('select.filter').change(function () {
$('select#'+this.id).val($(this).val());
$('div.active form.filter-form').trigger('submit');
});
How can I prevent the form submission (triggered by the select change event) from refreshing the page? 如何阻止表单提交(由select change事件触发)刷新页面?
You can simply put return false
on your markup of form. 您可以简单地在表单标记上添加
return false
。
<form onsubmit="function(); return false;">
Otherwise changing the button type to "button"
instead of "submit"
also should work. 否则,将按钮类型更改为
"button"
而不是"submit"
也应该有效。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.