最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - AJAX list update, get new elements and count - Stack Overflow

programmeradmin4浏览0评论

I have this HTML list

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

and a form to add new names via AJAX, multiple add separated by mas. The response is a list with the names

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

I'm trying to insert this names sorted alphabetically in the list, like this example /

This is my AJAX submit

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;
});

My question is, how I can update the value of listUsernames and numUsernames after adding an item?

I have this HTML list

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

and a form to add new names via AJAX, multiple add separated by mas. The response is a list with the names

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

I'm trying to insert this names sorted alphabetically in the list, like this example https://jsfiddle/VQu3S/7/

This is my AJAX submit

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;
});

My question is, how I can update the value of listUsernames and numUsernames after adding an item?

Share edited Apr 3, 2021 at 9:33 Syscall 19.8k10 gold badges43 silver badges59 bronze badges asked May 18, 2013 at 16:54 JGrinonJGrinon 1,4531 gold badge14 silver badges36 bronze badges 2
  • if ($(this).text() > user.name) { You know you're paring strings right ? – adeneo Commented May 18, 2013 at 17:14
  • Try this FIDDLE ? – adeneo Commented May 18, 2013 at 17:17
Add a ment  | 

3 Answers 3

Reset to default 1

You just need to update numUsernames at that point.

Add this where your ments are:

numUsernames = listUsernames.children().length;

listUsernames already has the updated children, as it's a reference to the parent element.

Edit: Re: your ment below:

This should probably work:

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

First you don't need a double jQuery wrapping:

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

listUsernames is already a jQuery object, so try:

$(htmlUser).appendTo(listUsernames);

And after every adding, you can update the numUsernames variable with:

numUsernames = listUsernames.children().length;

but this is not necessary because you can always access listUsernames.children().length in the success handler.

I update your 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?
});
发布评论

评论列表(0)

  1. 暂无评论