JavaScript 不适用于表单提交中的输入

标签 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.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/

相关文章:

javascript - 将固定的 div 保留在其容器中

javascript - 在事件索引 Accordion Bootstrap 中查找面板 ID

javascript - 了解 JavaScript 继承

javascript - 什么取代了 JavaScript 中已弃用的 __proto__

javascript - 无法将对象存储到 JavaScript 中的数组中

java - 如何在 Java 应用程序中嵌入 V8?

javascript - Md-tabs AngularJS 只工作一次

javascript 原型(prototype)与 this

javascript - 带有多个下拉菜单的 html,每个下拉菜单都有自己的显示/隐藏文本区域

javascript - 将数据从服务传递到 Controller