如何从其他功能调用此功能?

时间:2015-06-04 15:33:20

标签: javascript jquery html

我有一个搜索表单,想要当用户想要搜索并最后按回车键,然后指向他们想要的搜索页面。

HTML

<div class="search">        
    <form method="post" action="" onsubmit="return do_search();">
        <input type="text" value="Nhập từ khóa tìm kiếm" 
                       onfocus="if (this.value == 'Nhập từ khóa tìm kiếm') this.value = ''" 
                       onblur="if (this.value == '')this.value = 'Từ khóa'" id="searchInput" name="query" onkeydown="EnterKey(event);"/>
        <input type="submit" value="" />                            
    </form>                    
</div><!-- end #search -->

JS

function EnterKey(a) {
    if(13 == (window.event ? window.event.keyCode : a.which)){
        var temp = document.getElementById("searchInput").value;
        alert("enterkey: "+temp);
        do_search(temp);
   }
}

function do_search(a) {
     var input = document.getElementById("searchInput").value;
     alert("do_Search "+a);
     return  window.location.href = input,!1;
}

问题是,当我致电do_search(temp)时,提醒显示undefined

为什么我的提醒没有显示正确的消息?

2 个答案:

答案 0 :(得分:2)

事件onsubmit出错。您可以在没有参数的情况下调用函数do_search()。因此变量a未定义。

答案 1 :(得分:1)

问题是pressing enter会提交表单。所以它现在实际上被调用两次,按下输入并在EnterKey()内调用。您不必明确检测enter,然后拨打do_search(),因为按enter会自动拨打do_search()

相关问题