【问题标题】:JavaScript will not work with enter on form submitJavaScript 不适用于表单提交时的输入
【发布时间】:2017-07-28 10:06:04
【问题描述】:

当我按下搜索按钮时,我的 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);
//]]>

【问题讨论】:

    标签: javascript jquery prototype magento-1.9


    【解决方案1】:

    要使输入按键表单提交行为起作用,您需要在&lt;form&gt; 元素中添加submit 按钮。由于您当前的所有按钮都是通过 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>
    

    【讨论】:

      【解决方案2】:

      给按钮一些ID,然后使用如下代码:

          document.getElementById("id_of_button")
          .addEventListener("keyup", function(event) {
          event.preventDefault();
          if (event.keyCode == 13) {
              document.getElementById("id_of_button").click();
      

      //或提交您的表单。

          }
      });
      

      【讨论】:

        猜你喜欢
        • 2019-12-31
        • 1970-01-01
        • 2016-12-04
        • 2023-03-26
        • 2017-10-26
        • 2013-02-24
        • 2017-01-19
        • 2019-05-17
        • 2012-10-27
        相关资源
        最近更新 更多