路线没有被添加

时间:2017-11-09 06:51:21

标签: javascript html

我正在尝试使用搜索表单。但是当我点击输入时,它不会运行任何js部分而只是将index.php?srch=randomtext任何想法放在为什么它不会调用js?

<form name="sr" onsubmit="return GoToSearch();">
   <div class="search-icon">
     <input name="srch" id="srch" placeholder="Search The site" type="text">
   </div>
</form>

JS

function GoToSearch(){
   var srch = jQuery('#srch').val();
   window.location.href='index.php?route=product/search&search='+srch;
}

2 个答案:

答案 0 :(得分:1)

您的活动可能正在冒泡,默认的浏览器操作正在进行中。从您的函数返回false应该会阻止此操作。

&#13;
&#13;
function GoToSearch(){
  var srch = $('#srch').val();
  window.location = '/index.php?route=product/search&search='+srch;
  return false;
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form name="sr" onsubmit="return GoToSearch();">
  <div class="search-icon">
    <input name="srch" id="srch" placeholder="Search The site" type="text">
  </div>
</form>
&#13;
&#13;
&#13;

答案 1 :(得分:0)

如果您想使用jquery提交输入,那么您将需要表单 只需检测keyup代码然后GoToSearch试试这个:

&#13;
&#13;
$(function() {
$("#srch").on('keyup', function (e) {
    if (e.keyCode == 13) {
        alert('do search');
        GoToSearch();
    }
});
});
function GoToSearch(){
   var srch = jQuery('#srch').val();
   window.location.href='index.php?route=product/search&search='+srch;
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
   <div class="search-icon">
     <input name="srch" id="srch" placeholder="Search The site" type="text">
   </div>
&#13;
&#13;
&#13;