$(function () {
$('input:text:first').focus();
var $inp = $('input:text');
$inp.bind('keydown', function (e) {
var key = e.which;
if (key == 13) {
e.preventDefault();
var nxtIdx = $inp.index(this) + 1;
$(":input:text:eq(" + nxtIdx + ")").focus();
}
});
});上面这段代码是网上找的,部分符合需求,但我的页面中除了text还有radio、button、textarea、select等,请问怎么改写这段代码来实现页面的回车TAB顺序?TAB顺序
$('input:text:first').focus();
var $inp = $('input:text');
$inp.bind('keydown', function (e) {
var key = e.which;
if (key == 13) {
e.preventDefault();
var nxtIdx = $inp.index(this) + 1;
$(":input:text:eq(" + nxtIdx + ")").focus();
}
});
});上面这段代码是网上找的,部分符合需求,但我的页面中除了text还有radio、button、textarea、select等,请问怎么改写这段代码来实现页面的回车TAB顺序?TAB顺序
<input type="text" tabindex="2" >
$(function () {
$('input:text:first').focus();
var $inp = $('input,button,textarea,select');
$inp.bind('keydown', function (e) {
var key = e.which;
if (key == 13) {
e.preventDefault();
var nxtIdx = $inp.index(this) + 1;
$inp[(nxtIdx + 1) % $inp.length ].focus();
}
});
});
这段代码没多大问题,把text去掉就是了,关键是你的代码里控件的顺序是不是你想要的顺序
再按标识focus.
再说人家一般都用tab的,很小用回车模拟tab
<html lang="en">
<body>
<input type="password" tabindex=4/>
<input type="checkbox" name="" value="" tabindex=1 />
<input type="radio" name="" value="" tabindex=3 />
<input type="button" tabindex=2 /><textarea></textarea> </body>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script>
<script>
$(function () {
$('input:text:first').focus();
var $inp = $('input,button,textarea,select');
$inp.sort(function(a,b){ return (a.tabIndex||999) - (b.tabIndex||999) });
$inp.bind('keydown', function (e) {
var key = e.which;
if (key == 13) {
e.preventDefault();
var nxtIdx = $inp.index(this) + 1;
$inp[(nxtIdx + 1) % $inp.length ].focus();
}
});
});
</script>
</html>