简体   繁体   English

如何通过单击键盘按钮提交Web表单?

[英]How can I submit a web form by clicking keyboard button?

<form action="page.php" method="post">
    <input type="text" name="some_text" />
    <input type="submit" name="some_submit" /
</form>

I want to submit this form by pressing defined keyboard button. 我想按定义的键盘按钮提交此表单。 I wonder how to do it. 我不知道该怎么做。 If it was just an <a> element it would be simple - I would just change window.location value after handling keypress event. 如果它只是一个<a>元素,那将很简单-我将在处理keypress事件后更改window.location值。 But there is some data to send and I have no idea how to solve this. 但是有一些数据要发送,我不知道如何解决。

由于form.submit()的结果,数据将自动传递

You can create an event handler for a key press event, then submit the form using its submit() method to pass all the form data to the recipient page "page.php". 您可以为按键事件创建事件处理程序,然后使用其commit submit()方法提交表单,以将所有表单数据传递到收件人页面“ page.php”。

Using jQuery , this is trivial: 使用jQuery ,这很简单:

<form id="myForm" action="page.php" method="post">
    <input type="text" name="some_text" />
    <input type="submit" name="some_submit" />
</form>

<script type="text/javascript">

    $('#myForm").keyDown(function(e){

        if(e.which == 13) // When key pressed is "Enter" key.
            $('#myForm').submit();

    });

</script>

Read Javascript Madness: Keyboard Events for more information on interpreting keyboard events. 阅读Javascript Madness:键盘事件 ,了解有关解释键盘事件的更多信息。

Give a name to the form 给表格起个名字

<form name="myform" action="page.php" method="post"> 

After handling key press event do 处理按键事件后

document.myform.submit()

which will basically submit the form. 基本上会提交表格。 If you want to add more parameters to it add those as hidden elements inside form. 如果要向其中添加更多参数,请将其作为隐藏元素添加到表单中。

For an example of key press handling, see http://dev.kanngard.net/Permalinks/ID_20050426091851.html - it submits a form if key 13 (Enter) is pressed, but can be modified to any other key. 有关按键处理的示例,请参见http://dev.kanngard.net/Permalinks/ID_20050426091851.html-如果按下了按键13(回车),它将提交一个表格,但可以将其修改为任何其他按键。

In general: register a function to be called on KeyDown event, inside the function, check which key was pressed; 通常:注册一个要在KeyDown事件上调用的函数,在函数内部,检查按下了哪个键; if needed, submit() your form. 如果需要,请提交表单。

I'm in trouble: 我有麻烦了:

function action(button) {
if ($('a.'+button+':visible').length > 0) {
    if ($('a.'+button+':visible').attr('class') == button || $('a.'+button+':visible').attr('class') == 'rotate '+button) {
        var adr = $('a.'+button+':visible').attr('href');
        window.location = adr;
    }
    if ($('a.'+button+':visible').attr('class') != button) {
        $('a.'+button+':visible').click();
    }
}
    if ($('input.'+button+':visible').length > 0) {
        $('#form').submit();
    }
}

Where variable 'button' is button's classname and #form is form's id. 其中变量“按钮”是按钮的类名,而#form是表单的ID。 By clicking submit it works normally, but it doesn't work by keyboard event. 通过单击提交,它可以正常工作,但不能通过键盘事件来工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM