我正在尝试使用搜索表单。但是当我点击输入时,它不会运行任何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;
}
答案 0 :(得分:1)
您的活动可能正在冒泡,默认的浏览器操作正在进行中。从您的函数返回false
应该会阻止此操作。
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;
答案 1 :(得分:0)
keyup
代码然后GoToSearch
试试这个:
$(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;