当我按下搜索按钮时,我的 JS 代码将起作用。 但是当我使用 enter 提交表单时,下面的代码将不起作用。 在 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);
//]]>
最佳答案
要使 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>
关于JavaScript 不适用于表单提交中的输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45370615/