如何将焦点保持在按 Enter 提交表单的输入元素上

发布于 2024-12-07 12:57:49 字数 1211 浏览 0 评论 0原文

表单包含输入元素和提交按钮。按“提交”按钮会在浏览器的新选项卡中显示 pdf 文件。 如果按 Enter 键显示报告,焦点将移至提交按钮。选择报告参数标记后,需要再次单击输入元素才能向同一元素输入新值。

如何在按下 Enter 的位置保持焦点输入元素?

<form id="Form" class='form-fields' method='post' target='_blank'
action='Render'>

<input id='test' name='test' value='test' /> 
...
  <input id='_submit' type='submit' value='Show report' /> 
</form>

使用jquery、jqueryui、asp.net mvc2。

更新

我在表单中有 2 个具有相同名称的元素,以允许未选中的复选框提交:

<input type='hidden' value='false' name='Summary' />
<input type='checkbox' id='Summary' name='Summary' checked />
<select class="ui-widget-content ui-corner-all" id="_Report" name="_Report" size="10">
<option value="LV001">Report1</option>
<option value="LV003">Report2</option>
<option selected="selected" value="LV009">Report3</option>
</select>

我按照评论中的建议尝试了下面的代码。如果焦点位于复选框中,则不会恢复焦点。相反,选择元素是焦点。如何修复?

        $("#Form input:not([type=submit]), textarea, select").focus(function () {
            $('input').removeClass('hasFocus');
            $(this).addClass('hasFocus');
        });

Form contains input elements and submit button. Pressing Submit button shows pdf file in new tab in browser.
If enter is pressed to show report, focus moves to submit button. After selection report parameters tag again additional click in input element is required to enter new value to same element.

How to keep focus input element where enter was pressed ?

<form id="Form" class='form-fields' method='post' target='_blank'
action='Render'>

<input id='test' name='test' value='test' /> 
...
  <input id='_submit' type='submit' value='Show report' /> 
</form>

jquery, jqueryui, asp.net mvc2 are used.

update

I have 2 elements with same name in form to allow unchecked checkbox submission:

<input type='hidden' value='false' name='Summary' />
<input type='checkbox' id='Summary' name='Summary' checked />
<select class="ui-widget-content ui-corner-all" id="_Report" name="_Report" size="10">
<option value="LV001">Report1</option>
<option value="LV003">Report2</option>
<option selected="selected" value="LV009">Report3</option>
</select>

I tried code below as suggested in comment. If focus is in checkbox, it focus is not restored. Instead select element is focused. How to fix ?

        $("#Form input:not([type=submit]), textarea, select").focus(function () {
            $('input').removeClass('hasFocus');
            $(this).addClass('hasFocus');
        });

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

远昼 2024-12-14 12:57:49

如果您想返回到最后一个焦点项目,我认为您需要跟踪最后一个焦点项目。

像这样的东西可能会实现你正在寻找的东西:

$(function() {
    var lastFocus = [];

    $('#Form').find(':text,:radio,:checkbox,select,textarea').focus(function() {
        lastFocus = $(this);
    });

    $('#Form').submit(function(){    
        if (lastFocus.length == 1)
            lastFocus.focus();

        // just to prevent the actual submit - remove this to enable submit
        return false;
    });
});

我还制作了一个jsfiddle来测试: http://jsfiddle.net/ Jht6C/

If you want to get back to the last focused item, I think you will need to keep track on the last focused item.

Something like this will probably achieve what you are looking for:

$(function() {
    var lastFocus = [];

    $('#Form').find(':text,:radio,:checkbox,select,textarea').focus(function() {
        lastFocus = $(this);
    });

    $('#Form').submit(function(){    
        if (lastFocus.length == 1)
            lastFocus.focus();

        // just to prevent the actual submit - remove this to enable submit
        return false;
    });
});

I also made a jsfiddle to test: http://jsfiddle.net/Jht6C/

め七分饶幸 2024-12-14 12:57:49

或者,由于您已经在使用 jQuery,您也可以使用这个简单的函数将焦点设置在提交上:

$(function() {

    $('#Form').submit(function(){    

        $('#test').focus();

    });

});

这样,一旦提交表单,焦点就会设置为“#test”。

演示: http://jsfiddle.net/vZSZT/

编辑:

将焦点返回到表单时的位置已提交,我可能会这样做:

$(function() {

    //Lets make sure we dont target the submit-input
    $("input:not([type=submit])").focus(function () {

        //Remove previous focused inputs
        $('input').removeClass('hasFocus');    

        //Add focus class to this input
        $(this).addClass('hasFocus');

    });


    $('#Form').submit(function(){    

        $('.hasFocus').focus();


    });

});

演示:http://jsfiddle.net/vZSZT/4/

然而,Christoffer 的方式似乎也不错(甚至可能更好?我不知道),但两者都会起作用:)

Or since you are already using jQuery, you could probably also use this simple function to set focus on submit:

$(function() {

    $('#Form').submit(function(){    

        $('#test').focus();

    });

});

This way focus will be set to "#test" as soon as the form is submitted.

Demo: http://jsfiddle.net/vZSZT/

Edit:

To get focus back to where it was when form was submitted, I would probably do it this way:

$(function() {

    //Lets make sure we dont target the submit-input
    $("input:not([type=submit])").focus(function () {

        //Remove previous focused inputs
        $('input').removeClass('hasFocus');    

        //Add focus class to this input
        $(this).addClass('hasFocus');

    });


    $('#Form').submit(function(){    

        $('.hasFocus').focus();


    });

});

Demo: http://jsfiddle.net/vZSZT/4/

However, Christoffer´s way seems to be good too (and probably even better? I cant tell), but both will work :)

凑诗 2024-12-14 12:57:49

实际上,有一个简单的解决方案可以在表单提交后聚焦。将关键字 autofocus 作为属性添加到输入元素。

<input id='test' name='test' value='test' autofocus>

Actually, there is a simple solution for focusing after form submit. Add the keyword autofocus as an attribute to the input-element.

<input id='test' name='test' value='test' autofocus>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文