按下enter键让提交页面的某个方法

105 阅读1分钟

最近做一个前端页面,在一个分页和查询里面,要用enter键会自动提交,先提交查询的内容,如果查询为空再提交分页的页数

\

\

代码如下

document.onkeydown = function (e) {
    if (!e) e = window.event;
    if ((e.keyCode || e.which) == 13) {
        toQuote();
        //提交按钮触发的方法
    }
};

function toQuote(){
    var url=" ${contextPath } /info/xsb_tzmx.htm";
    var queryTime=$("#queryTime").val();
    var endDate=$("#endDate").val();
    var page=$("#pageGoValue").val();
    if(queryTime!=null&&queryTime.length>0){
        url+="?queryTime="+queryTime;
        if(endDate!=null&&endDate.length>0){
            url+="&endDate="+endDate;
        }
    }else {
        if(endDate!=null&&endDate.length>0){
            url+="?endDate="+endDate;
        }else if(page!=""){
            pageGo();
            return;
        }else {
            alert("查询时间不能为空!");
            return;
        }
    }
    window.location.href=url;
}

\