2014-11-15 13 views
5

Tôi có biểu mẫu html và bất cứ khi nào enter được nhấn vào trường tiếp theo sẽ được chọn.
đây là mã.Chọn trường tiếp theo khi nhấn enter bằng cách sử dụng jquery

$(document).on("keydown","input",function(event) { 
 
    \t if (event.which === 13) { 
 
    \t \t event.stopPropagation(); 
 
    \t \t event.preventDefault(); 
 
    \t \t $(this).nextAll("input").eq(0).focus(); 
 
    \t } 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<form> 
 
    \t <table> 
 
    \t \t <tr> 
 
    \t \t \t <td>Medical Record No.</td><td><input type="text" name="labNo" /></td> 
 
    \t \t </tr> 
 
    \t \t <tr> 
 
    \t \t \t <td>Age/sex </td><td><input type="text" name="age" /> 
 
    \t \t \t \t <select id="age"> 
 
    \t \t \t \t \t <option value="Year">Year</option> 
 
    \t \t \t \t \t <option value="Month">Month</option> 
 
    \t \t \t \t </select> 
 
    \t \t \t \t <select id="sex"> 
 
    \t \t \t \t \t <option value="Male">Male</option> 
 
    \t \t \t \t \t <option value="Female">Female</option> 
 
    \t \t \t \t </select> 
 
    \t \t \t </td> 
 
    \t \t </tr> 
 
    \t \t <tr> 
 
    \t \t \t <td>Phone </td> 
 
    \t \t \t <td><input type="text" name="" value="-,-" /></td> 
 
    \t \t </tr> 
 
    \t \t <tr> 
 
    \t \t \t <td colspan="2"><input type="button" id="patBtn" value="Save (S)" accesskey="s" /> 
 
    \t \t \t \t <input type="reset" value="Set Default (D)" accesskey="d" /> 
 
    \t \t \t </td> 
 
    \t \t </tr> 
 
    \t </table> \t 
 
    </form>

Đây không phải đang làm việc.

Trả lời

0

này nên làm việc không có vấn đề cấu trúc html

$(document).on("keydown","input",function(event) { 
if (event.which === 13 || event.keyCode === 13) { 
    event.stopPropagation(); 
    event.preventDefault(); 
    var position = $(this).index('input'); 
    $("input").eq(position+1).focus(); 
} 
}); 

cũng cho chọn:

$(document).on("keydown, keyup","input, select",function(event) { 
if (event.which === 13 || event.keyCode == 13) { 
    event.stopPropagation(); 
    event.preventDefault(); 
    var position = $(this).index('input, select'); 
    $("input, select").eq(position+1).focus(); 
} 
}); 

jsfiddle: http://jsfiddle.net/xh0m7pzu/1/

+0

làm cách nào để tùy chọn 'chọn' cũng có thể. @A_funs – Axeem

+0

hoạt động trong firefox và safari –

+0

thực sự tôi đã thay đổi, thêm khóa cũng có vẻ hoạt động trong tất cả các trình duyệt hiện tại –

1

Hãy thử sau kịch bản

$(document).on("keydown","input",function(event) { 
    debugger; 
    if (event.which === 13) { 
     event.stopPropagation(); 
     event.preventDefault(); 
     $(this).parents("tr").next().find("input").focus(); 
    } 
}); 

Demo

Các vấn đề liên quan