顺序执行事件侦听器,然后按enter键提交表单
sequential execution of event listener and then form submission by pressing enter key
我有一个事件侦听器和一个表单。当用户按下回车键时,两者都会启动。我想要的是当用户按下回车键,然后第一个事件监听器应该执行,然后表单提交。那么该怎么做呢?
google.maps.event.addListener(autocomplete, 'place_changed', function() {
// fires when enter key is pressed
infowindow.close();
marker.setVisible(false);
input.className = '';
var place = autocomplete.getPlace();
if (!place.geometry) {
// Inform the user that the place was not found and return.
input.className = 'notfound';
return;
}
}
and
<form action="xyz.php" method="post">
//submits when enter key is pressed
<div id="searchWrapper" style="width:940px;margin-bottom:10px">
<input name="txtSearch" type="text" id="txtSearch" class="search focus" placeholder="Where do you need parking?" value=<?php echo '"'.$str.'"'; ?> />
<input class="dates search search-date" style="height:30px;background-image:url('images/sprite-master.png') ;background-position:-480px 0px; cursor:pointer;width:110px;" name="txtSearchStartDate" type="text" id="txtSearchStartDate" placeholder="today" enddate="txtSearchEndDate" />
<input name="txtSearchEndDate" type="text" id="txtSearchEndDate" class="dates search search-date" placeholder="(optional)" />
<input type="submit" value=" Search" id="btnSearch" class="btn btn-primary" />
<input name="city" id="city" type="hidden" value="">
<input name="state" id="state" type="hidden" value="">
<input name="country" id="country" type="hidden" value="">
<input name="lat" id="lat" type="hidden" value="">
<input name="long" id="long" type="hidden" value="">
</div>
</form>
停止form onsubmit函数提交表单,检查是否在place_changed
事件中给出了一个位置。如果是,手动提交表单,否则更改输入的类名。
var form = document.getElementById("searchForm"), // I assume the form tag has the ID searchForm
input = document.getElementById("txtSearch"),
autocomplete = new google.maps.places.Autocomplete(input);
form.onsubmit = function(event) {
// stop the form from submitting
event.preventDefault();
};
google.maps.event.addListener(autocomplete, 'place_changed', function() {
input.className = '';
var place = autocomplete.getPlace();
if (!place.geometry) {
// Inform the user that the place was not found and return.
input.className = 'notfound';
} else {
input.className = '';
// this won't fire the onsubmit function
form.submit();
}
});
相关文章:
- 当阻止Enter键提交AJAX表单时,关注下一个输入是't工作
- 按enter键时,jQuery UI自动完成字段未提交
- 如何通过按Enter提交输入表格,但在按shift和Enter时不提交
- 当我有多个可能的提交按钮时,如何在ENTER上进行表单提交
- 使用javascript在Enter键上提交表单
- 使用enter提交表单,而不触发页面刷新
- 单击按钮时提交 OnClick 事件,但在键盘上按 Enter 时不提交
- 使用 ENTER 键提交表单在 Angular 中不起作用
- 使用 Enter 提交表单会在插入输入字段后中断 - 原因以及如何修复
- Google 地方自动填充place_changed事件会在提交表单后按 Enter 键触发
- 修改 jQuery 自动完成,不要在 Enter 上急切提交
- Ajax使用enter和onclick提交表单
- 当焦点在TextArea上时,按enter键提交表单
- 使用ENTER键提交的Wordpress表单不起作用
- 使用Shift+Enter提交表单
- 允许Shift + Enter提交表单
- 按键盘ENTER提交ajax请求
- 按钮点击工作正常,但不希望的行为,当按下enter提交表单
- 如何创建一个新的文本区域,当用户使用shift+enter提交
- 在文本框中,当我按ENTER提交表单时,当我按shift+ENTER插入新行