开发者

Run the same ajax event multiple times from sibling objects

I have a script for changing positions of objects on some list. When changing value of input an ajax call to a function changing position in the db is made, and an updated list is returned. But if I want to update my list once again I need to refresh the page. How can I reattach event to element?

js:

$(".object-position").livequery("change", function() {
    $("#objects-list input").attr('disabled', true);
    var action = $(this).attr('name');
    var position = $(this).attr('value');
    var id = $(this).attr("id");
    var model = id.split("-")[0];
    var object_id = id.split("-")[1];

    $("#loader").show();
    $("#loader").fadeIn(200);
    
    $.ajax({
        type: "POST",
        url: "/manage/update_position/",
        data: "action=" + action + "&model=" + model + "&object_id=" + object_id + "&position=" + position,
        dataType: "json",
        success: function(da开发者_JAVA技巧ta){
            $("#loader").fadeOut("fast", function () {
                $("#loader").hide();
            });
            $("#objects-list").html(data["html"]);
            $("#message").show();
            $("#message").fadeIn(400).html('<span>'+data["message"]+'</span>');
            setTimeout(function(){
                $("#message").fadeOut("slow", function () {
                    $("#message").hide();
                });
            }, 1500); 
        }
    });
    $("#objects-list input").attr("disabled", false);
    return false;
});

and my sample page :

LINK


Bind/delegate the event to a containing element that doesn't get refreshed or removed. For example:

$('#objects-list').delegate('.object-position', 'change', function() {
  ... code ...
});

How delegate works - a single event is bound to '#objects-list' but is triggered for any event target of type '.object-position'. Also, it is the only function in jQuery where 'this' doesn't refer to what is enclosed in $(...).

More on event delegation.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜