将焦点设置为<select>元素</select>

时间:2014-12-23 07:48:11

标签: javascript jquery html

我有一个注册表单,第一个elementtextbox,第二个elementselect

当我按下回车键时,我想将焦点设置在select元素。

$('#newUserId').keypress(function(event){
    if (event.which == 13)
    $('#newUserId:first').focus();
});

我也环顾四周,甚至this linkthis link也无济于事,当我按下回车键时,它什么也没做。但是当我检查将焦点设置在其他textbox元素上时,它确实改变了焦点。

我的HTML表单:

<form name='formCreateNewUser' id='formCreateNewUser' method='post' action=''>
        <label> Create New User </label> <br />
           <input autocomplete='off' name='newUserId' id='newUserId' class='newUser' 
              type='text' size='17' maxlength='17' placeholder='ID No.' 
        /> <span id='newUserIdResult'> </span><br /><br />
        <select name='newUserType' id='newUserType' class='newUser'>
           <option class='newUser' value=0>User Type</option>
           <option class='newUser' value=1>Student</option>
           <option class='newUser' value=2>Teacher</option>
           <option class='newUser' value=5>Admin</option>
        </select> <br /><br />
        <input autocomplete='off' name='newUserPass' id='newUserPass' class='newUser' 
            size='17' maxlength='17' placeholder='Password' type='password' 
        /> <br /><br />
        <input autocomplete='off' name='newUserConfPass' id='newUserConfPass' class='newUser' 
           size='17' maxlength='17' placeholder='Confirm Password' type='password' 
        /> <br /><br />
        <input autocomplete='off' name='newUserFirstName' id='newUserFirstName' class='newUser' 
           size='17' maxlength='17' placeholder='First Name' type='text' 
        /> <br /><br />
        <input autocomplete='off' name='newUserMiddleName' id='newUserMiddleName' class='newUser' 
           size='17' maxlength='17' placeholder='Middle Name' type='text' 
        /> <br /><br />
        <input autocomplete='off' name='newUserLastName' id='newUserLastName' class='newUser' 
           size='17' maxlength='17' placeholder='Last Name' type='text' 
        /> <br /><br />
        <button name='newUserSubmit' id='newUserSubmit' class='operationstylebutton' type='button' >Create User</button>
        </form>

2 个答案:

答案 0 :(得分:2)

http://jsfiddle.net/o11vowsp/

$('#newUserId').on('keyup', function(e) {
    if ( e.keyCode == 13 )
    $('#newUserType').focus();
});

答案 1 :(得分:2)

Demo添加了一个用于突出显示效果的类

$('#newUserId').keypress(function (event) {
    if (event.which == 13)
    {$('#newUserType').addClass("highlght").focus();}
});