Javascript 为什么单击输入类型submit时从不调用is on('submit')函数?
昨天一切都很好,但今天我做了一些愚蠢的事情,提交的jquery回调崩溃了 首先,jQuery在类路径上,其他回调也在工作。但我无法使用jQuery注册提交事件的回调。我的回拨:Javascript 为什么单击输入类型submit时从不调用is on('submit')函数?,javascript,jquery,html,Javascript,Jquery,Html,昨天一切都很好,但今天我做了一些愚蠢的事情,提交的jquery回调崩溃了 首先,jQuery在类路径上,其他回调也在工作。但我无法使用jQuery注册提交事件的回调。我的回拨: var $form = $('#hostelSearchForm'); $form.on('submit', function(e) { //console.log($('#hostelSearchFormButton').attr('id')); hostelSe
var $form = $('#hostelSearchForm');
$form.on('submit', function(e) {
//console.log($('#hostelSearchFormButton').attr('id'));
hostelSearchJson = collectHostelSearchFormData($form);
console.log(JSON.stringify(hostelSearchJson));
//do ajax search
hostelSearch(hostelSearchJson);
//serializing is bad because of checkboxes value as on and off
// console.log(JSON.stringify($form.serialize()));
e.preventDefault();
return false;
});
此表单包含提交类型的相应输入:
<form:form id="hostelSearchForm" modelAttribute="hs" method="POST"
cssClass="mainForm">
.........................
<div>
<fmt:message key="searchButtonLabel" var="searchButtonLabel" />
<input id="hostelSearchFormButton" type="submit" class="btn btn-primary"
style="font: bold 18px Tahoma, serif; padding: 15px"
value="${searchButtonLabel}" />
<button type="reset" class="btn">
<fmt:message key="cancel" />
</button>
</div>
</form:form>
在chrome中,我看到它以text/html作为媒体类型发出post请求
即使我在hostelSearchFormButton上附加回调,它也不会被触发。但此页面上的所有其他回调都运行良好
这是一个愚蠢的错误,我不明白是什么错了?很好的问题
这种情况会发生,因为submit首先在javascritp之后使用HTML,所以当您单击它时,根据默认的HTML发送表单数据,为避免这种影响,请在表单标记onsubmit=return false中添加如下内容:
<form action="my.php" onsubmit="return false">
再见那么你想在jquery中做的事情是如何被解雇的呢?
<form action="my.php" onsubmit="return false">
$('form').submit();