输入按钮不想将表单提交给iframe

时间:2013-03-22 12:53:53

标签: javascript html forms iframe form-submit

我有这个表单,我需要在按下ENTER时提交。一切看起来都很好,但它没有用。当点击搜索按钮工作正常但我也需要使用ENTER按钮。

HTML code:

<form id="SearchForm" action="mysite.com/search" target="my-iframe" method="post">


<input type="text" id="x" class="dimmed" name="keywords[all_words]" />


<div class="fieldbg"><input type="text" id="y" class="dimmed" name="City[all_words]" /></div>



<type class="btn_search row_right" onclick="document.getElementById('SearchForm').submit()">

我尝试使用此脚本,不起作用:

<script>
window.onload = function() {
   var textbox = document.getElementById('x');
    textbox.onkeypress = function (e) {
        if (e.keyCode === 13) {
            document.getElementById('SearchForm').submit();
        }
    };
}
</script>

3 个答案:

答案 0 :(得分:2)

首先,您的链接不正确:403 Forbidden;

第二,请检查页面上是否定义了SearchForm ID :)

答案 1 :(得分:2)

您可以监控键盘输入并在输入命中时提交表单:

<script>
    window.onload = function() {
       var textbox = document.getElementById('ID_OF_INPUT_FIELD_HERE');
        textbox.onkeypress = function (e) {
            if (e.keyCode === 13) {
                document.getElementById('SearchForm').submit();
            }
        };
    }
</script>

答案 2 :(得分:2)

确保您的代码无法在IE上运行,请尝试以下方法:

<script>
window.onload = function() {
   var textbox = document.getElementById('x');
    textbox.onkeypress = function (e) {
        e = e || window.event;
        if (e.keyCode === 13) {
            document.getElementById('SearchForm').submit();
        }
    };
}
</script>