JavaScript无法在表单提交时使用enter
当我按下搜索按钮时,我的JS代码就会工作。 但当我使用enter提交表单时,下面的代码将不起作用。 在magento中使用代码。 HtmlJavaScript无法在表单提交时使用enter,javascript,jquery,prototype,magento-1.9,Javascript,Jquery,Prototype,Magento 1.9,当我按下搜索按钮时,我的JS代码就会工作。 但当我使用enter提交表单时,下面的代码将不起作用。 在magento中使用代码。 Html <form action="<?php echo $this->getUrl('storelocator/index/searchbydistance') ?>" method="GET" id="search_by_distance"> <button type="button" onclick="storeSearch
<form action="<?php echo $this->getUrl('storelocator/index/searchbydistance') ?>" method="GET" id="search_by_distance">
<button type="button" onclick="storeSearch.submit(this)"><?php echo $this->__('Search') ?></button></form>
要使enter键按下表单提交行为正常工作,您需要在
元素中设置提交
按钮。由于当前按钮所做的只是通过JS提交表单,因此您只需更改其类型
:
<form action="<?php echo $this->getUrl('storelocator/index/searchbydistance') ?>" method="GET" id="search_by_distance">
<button type="submit"><?php echo $this->__('Search') ?></button>
</form>
<script type="text/javascript">
storeSearch.submit = function(button, url) {
alert('hi');
}.bind(storeSearch);
</script>
为按钮提供一些id,然后使用如下代码:
document.getElementById("id_of_button")
.addEventListener("keyup", function(event) {
event.preventDefault();
if (event.keyCode == 13) {
document.getElementById("id_of_button").click();
//或者提交你的表格
}
});
}
});