jquery排序行和值

时间:2011-08-15 16:45:51

标签: jquery sorting rows

基于此前。 http://jsfiddle.net/vaDkF/160/当点击UP或DOWN维护命令(1.2.3.4 ...)的值时,如何更改输入值以增加或减少值?另外如何添加另一个按钮来删除行?

2 个答案:

答案 0 :(得分:1)

$(document).ready(function(){
    $(".up,.down").click(function(){
        var row = $(this).closest("tr");
        if ($(this).is(".up")) {
            row.prev().before(row);
        } else if ($(this).is(".down")) {
            row.next().after(row);
        }

        ResetSort();
    });

    $('.down').after(' <a href="#" class="delete">Delete</a>');
    $('.delete').click(function() {
        $(this).closest('tr').remove();
    });
});

function ResetSort() {
    $(':hidden[name="sort"]').each(function (i,v) {
        $(v).val(i + 1);
    });
}

以下是一个工作示例:http://jsfiddle.net/JUh74/5/

更新:用fudgey

指出,用最近的('tr')替换父母('tr:first')

答案 1 :(得分:0)

哎呀......我误读了你的问题。您想按顺序保留“值”或“文本”吗?

在点击功能中添加:

$('input').each(function(i){
    $(this).val(i+1);
});

所以你最终得到这样的东西:

$(document).ready(function(){
    $(".up,.down").click(function(){
        var row = $(this).parents("tr:first");
        if ($(this).is(".up")) {
            row.insertBefore(row.prev());
        } else {
            row.insertAfter(row.next());
        }
        // renumber input values (not the text)
        $('input').each(function(i){
            $(this).val(i+1);
        });
    });
});