使用箭头键在输入框中导航和选择文本

时间:2012-11-22 10:23:38

标签: javascript jquery

作为解决问题的解释: Using arrows-keys to navigate

http://jsfiddle.net/BdVB9/

我有一些文本输入相同的表格,在单元格之间导航期间,我在表格单元格的输入框中选择文本时遇到了一些问题。

任何人都可以帮我解决吗?导航工作正常,但没有在输入框中选择文本!而且我想只在具有输入框的单元格之间导航,而不是所有它们

注意:我只想在具有文本输入框的单元格之间导航。

表格代码:

    <table border="1" id="navigate">
    <tbody>
        <tr>
            <td><input type="text" id="1" class="input"></td>
            <td></td>
            <td><input type="text" id="3" class="input"></td>
            <td></td>
            <td><input type="text" id="5" class="input"></td>
        </tr>
        <tr>
            <td><input type="text" id="6" class="input"></td>
            <td><input type="text" id="7" class="input"></td>
            <td></td>
            <td><input type="text" id="9" class="input"></td>
            <td><input type="text" id="10" class="input"></td>
        </tr>
        <tr>
            <td><input type="text" id="11" class="input"></td>
            <td><input type="text" id="12" class="input"></td>
            <td></td>
            <td><input type="text" id="14" class="input"></td>
            <td><input type="text" id="15" class="input"></td>
        </tr>
    </tbody>
</table>

and this is my own demon

2 个答案:

答案 0 :(得分:4)

我把你指定的功能http://jsfiddle.net/tppiotrowski/L7cm8/10/放在一起。我希望我能正确理解你的要求。如果您需要任何更改或不理解代码,请告诉我们。祝你好运!

var active = 0;
//$('#navigate td').each(function(idx){$(this).html(idx);});
rePosition();

$(document).keydown(function(e) {
    reCalculate(e);
    rePosition();
    // if key is an arrow key, don't type the user
    // input. if it is any other key (a, b, c, etc)
    // edit the text
    if (e.keyCode > 36 && e.keyCode < 41) {
        return false;
    }
});

$('td').click(function() {
    active = $(this).closest('table').find('td').index(this);
    rePosition();
});


function reCalculate(e) {
    var rows = $('#navigate tr').length;
    var columns = $('#navigate tr:eq(0) td').length;
    var temp;

    if (e.keyCode == 37) { //move left or wrap
        temp = active;
        while (temp > 0) {
            temp = temp - 1;
            // only advance if there is an input field in the td
            if ($('#navigate tr td').eq(temp).find('input').length != 0) {
                active = temp;
                break;
            }
        }
    }
    if (e.keyCode == 38) { // move up
        temp = active;
        while (temp - columns >= 0) {
            temp = temp - columns;
            // only advance if there is an input field in the td
            if ($('#navigate tr td').eq(temp).find('input').length != 0) {
                active = temp;
                break;
            }
        }
    }
    if (e.keyCode == 39) { // move right or wrap
        temp = active;
        while (temp < (columns * rows) - 1) {
            temp = temp + 1;
            // only advance if there is an input field in the td
            if ($('#navigate tr td').eq(temp).find('input').length != 0) {
                active = temp;
                break;
            }
        }
    }
    if (e.keyCode == 40) { // move down
        temp = active;
        while (temp + columns <= (rows * columns) - 1) {
            temp = temp + columns;
            // only advance if there is an input field in the td
            if ($('#navigate tr td').eq(temp).find('input').length != 0) {
                active = temp;
                break;
            }
        }
    }
}

function rePosition() {
    console.log(active);
    $('.active').removeClass('active');
    $('#navigate tr td').eq(active).addClass('active');

    var input = $('#navigate tr td').eq(active).find('input').focus();
    scrollInView();
}

function scrollInView() {
    var target = $('#navigate tr td:eq(' + active + ')');
    if (target.length) {
        var top = target.offset().top;

        $('html,body').stop().animate({
            scrollTop: top - 100
        }, 400);
        return false;
    }
}​

答案 1 :(得分:2)

看看这篇JQFAQ帖子How to select a table cell using click or navigation keys? 这有你想要的东西。

相关问题