为什么当点击输入类型submit时,on('submit')函数从不被调用?
Why is on('submit') function is never called when clicking on input type submit?
昨天一切都很好,但今天我做了一个愚蠢的事情,onsubmit jquery回调崩溃了。
首先,jQuery是在类路径和其他回调工作。但我不能注册回调提交事件与jQuery。我的回调:
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;
});
这个表单对应的输入类型是submit:
<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>
当我点击hostelSearchFormButton
时,它将我重定向到新页面并给出
415 Unsupported Media Type.
在chrome中,我看到它使post请求与text/html
作为媒体类型。
即使我在hostelSearchFormButton
上附加回调,它也不会被触发。但是本页上所有其他回调都运行良好。
这是如此愚蠢的错误,我不明白是怎么回事?
问得好,
这会发生,因为提交在第一个实例中使用HTML在javascript之后,所以当你点击它,默认HTML发送表单数据,以避免这种影响添加在你的表单标签onsubmit="return false"像这样:
<form action="my.php" onsubmit="return false">
当你在jquery中完成你想做的事情时,你可以用
发送数据$('form').submit();
再会
相关文章:
- jQuery函数无法处理Form Submit
- 函数中的 .submit() 工作 2 次
- Ajax Post-Submit函数提交两次
- .submit JS函数在Safari中不显示CSS覆盖(除非我取消页面加载)
- jQuery函数.submit()不工作
- 如何防止我的$('form').submit(函数(事件))应用于某些表单
- jQuery-Submit函数不会发布值
- jquery-submit函数不起作用
- 无法调用 $scope.submit 函数
- 如何将视图中的模型变量设置为$(“form”).submit函数内javascript中的值
- 在jQuery.submit函数中更新并显示一个变量
- 提交表单时未调用Submit函数
- .submit函数在使用kendo验证器无效时未运行
- 为什么不't我的angular submit函数添加了空数据
- Submit()函数不工作
- 为什么当点击输入类型submit时,on('submit')函数从不被调用?
- 为什么ng-submit没有启动$scope.submit函数
- .submit()函数在ajax()函数中不起作用
- submit函数在第一次提交jquery之后提交两次,PHP
- Submit()函数干扰提交事件