日期:2014-05-16  浏览次数:20342 次

js提交表单打开新页面

?

<input type="text" id="version" onkeydown="checkKeyCode(event)">
<input type="button" id="openButton" onclick="myfun()" value="openButton"/>
<div style="display: none;">
    <form id="testForm" action="ieCheck.jsp" method="get" target="_bland">  //注意这里是_blank,也就是打开新页面
    </form>
</div>

?

? ? 针对上面的HTML,也就是text和button都不在form中,此时对应的js:

?

function checkKeyCode(e){
    e = e || event;
    if(e.keyCode==13){
         $("#testForm").submit();
    }
}
function myfun(){
    $("#testForm").submit();
}

?

?当在text中按下回车键的时候,$("#testForm").submit()提交表单,此时打开新页面会被IE拦截(监听的是onkeydown事件,onclick事件有所不同

?

?当点击button的时候, $("#testForm").submit();提交表单,此时打开新页面不会被IE拦截

?

然后将text和button放到form中:

?

?

<div style="display: none;">
    <form id="testForm" action="ieCheck.jsp" method="get" target="_bland">
        <input type="text" id="version" onkeydown="checkKeyCode(event)">
        <input type="button" id="openButton" onclick="myfun()" value="openButton"/>
    </form>
</div>

?

?

如上,此时text和button都是form中的元素

?

?当在text中按下回车键的时候,$("#testForm").submit()提交表单,此时打开新页面,IE会弹出拦截提示框,但有时候能打开,有时候打不开新页面(监听的是onkeydown事件,onclick事件有所不同

?

?当点击button的时候,?$("#testForm").submit();提交表单,此时打开新页面不会被IE拦截

?

所以比较坑爹,用button提交的话,不会被拦截,用text监听回车事件提交的话,可能会被拦截。。。尼玛