AJAX列表更新,获取新元素和计数

时间:2013-05-18 16:54:45

标签: javascript ajax jquery

我有这个HTML列表

<ul id='usernameList'>
    <li class='username'>John</li>
    <li class='username'>Mark</li>
</ul>

和一个通过AJAX添加新名称的表单,多个用逗号分隔。响应是一个名称为

的列表
[{name:David, newUser:yes}, {name:Sara, newUser:yes}, {name:Mark, newUser:no}]

我正在尝试按字母顺序在列表中插入此名称,例如此示例http://jsfiddle.net/VQu3S/7/

这是我的AJAX提交

var form = $('#formUsername');
form.submit(function () {
$.ajax({
    type: form.attr('method'),
    url: form.attr('action'),
    data: form.serialize(),
    dataType: "json",
    beforeSend: function () {
        //
    },
    success: function (data) {

        var listUsernames = $('#usernameList');
        var numUsernames = listUsernames.children().length;

        $.each(data, function(i, user) {
            if(user.newUser == "yes"){

                var htmlUser = "<li class='username'>" + user.name + "</li>";
                var added = false;

                $(".username", listUsernames).each(function(){
                    if ($(this).text() > user.name) {
                        $(htmlUser).insertBefore($(this));
                        added = true;
                    }

                });

                if(!added)
                    $(htmlUser).appendTo($(listUsernames));

            }

            // HERE I DO alert('numUsernames')
            // I get the same number of users before sending form

            // How can I update here the value of listUsernames and numUsernames?
        });



    }
});
return false;
});

我的问题是,如何在添加项目后更新listUsernames和numUsernames的值。

3 个答案:

答案 0 :(得分:1)

首先,您不需要双jQuery包装:

$(htmlUser).appendTo($(listUsernames));

listUsernames已经是一个jQuery对象,所以请尝试:

$(htmlUser).appendTo(listUsernames);

每次添加后,您都可以使用以下内容更新numUsernames变量

numUsernames = listUsernames.children().length;

但这不是必需的,因为您始终可以访问listUsernames.children().length处理程序中的success

答案 1 :(得分:1)

您只需要在此时更新numUsernames

将您的评论添加到:

numUsernames = listUsernames.children().length;

listUsernames已经有了更新的子元素,因为它是对父元素的引用。

编辑:回复:您的评论如下:

这应该可行:

$(".username", listUsernames).each(function(){
    if ($(this).text() > user.name) {
        $(htmlUser).insertBefore($(this));
        added = true;
        return false; // stop `.each` loop.
    }
});

答案 2 :(得分:0)

我更新了您的JSFIDDLE

var listUsernames = $('#usernameList');
var numUsernames = listUsernames.children().length;
 var data = [{name:'David', newUser:'yes'}, {name:'Sara', newUser:'yes'}, {name:'Mark', newUser:'no'}]
$.each(data, function(i, user) {
    if(user.newUser == "yes"){

        var htmlUser = "<li class='username'>" + user.name + "</li>";
        var added = false;

        $(".ingredient", listUsernames).each(function(){
            if ($(this).text() > user.name) {
                $(htmlUser).insertBefore($(this));
                added = true;
            }

        });

        if(!added)
            $(htmlUser).appendTo($(listUsernames));

    }

    // HERE I DO alert('numUsernames')
    // I get the same number of users before sending form

    // How can I update here the value of listUsernames and numUsernames?
});