溫課

JS如何判斷文本框内的回車?

举个例子, 您可以先在网页底部书写类似这样的JS代码, 封装成search方法:

function search() {
    var e = event || window.event || arguments.callee.caller.arguments[0];
    if(e && e.keyCode==13){ // 回车按键代码
        var name = $('#name').val(),
            practice = $('#practice').val(),
            office = $('#office').val();
        window.location.href = '/index/advancedsearch?name='+name+'&practice='+practice+'&office='+office;
    }
}

然后在想监听的文本框的dom元素里面加上一句onkeypress="search();"就可以了。例如:

<input type="text" placeholder="开始搜索" onkeypress="search();" />

精彩推薦

我要說兩句
網上嘉賓點評
(沙發已空)