当我按下搜索按钮时,我的JS代码将起作用。 但是当我使用enter提交表单时,Bellow代码将无效。 在magento中使用代码。 的 HTML
<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>
的Javascript
<script type="text/javascript">
//<![CDATA[
storeSearch.submit = function(button, url) {
alert('hi');
}.bind(storeSearch);
//]]>
答案 0 :(得分:1)
要使用enter keypress表单提交行为,您需要在submit
元素中使用<form>
按钮。正如您当前所有按钮所做的那样是通过JS提交表单,您只需更改其type
:
<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>
答案 1 :(得分:0)
给按钮一些id,然后使用如下代码:
document.getElementById("id_of_button")
.addEventListener("keyup", function(event) {
event.preventDefault();
if (event.keyCode == 13) {
document.getElementById("id_of_button").click();
//或提交表格。
}
});