JSON对象无法正确更新

时间:2013-03-05 12:21:15

标签: html jquery

首先,我不确定我的标题是否正确描述了问题...我确实搜索了但没有找到任何帮助我的东西......

我正在处理的项目有一个#orderList。所有订单都有删除选项。订单被删除后,列表会更新。

听起来很简单......我遇到了一个问题。

/**
 * Data returned at the end of selecting some options
 */
$.post(myUrl, $('#myForm').serialize(), function(data) {
    // I build the orderlist
    // The data returned is a JSON object holding session data (including orders)
    buildOrderList(data);
    ...
    // Do some other work
});

/*
 * function to build the html list
 */
function buildOrderList(data) {
    // Empty list
    $('#orderList').empty();

    // The click handler for the delete button is in here because it needs the data object
    $(document).on('click', '[id^=delete_]', function() {
        // Get the orderId from the delete button
        var orderId = $(this).attr('id').split('_');
        orderId = orderId['1'];
        // I call the delete function
        deleteOrder(orderId, data);
    });

    var html = '';
    // Loop the data object
    $.each(data, function(key,val){
        ...
        // Put html code needed in var html
        ...
    });
    $('#orderList').append(html);
}

/*
 * function to delete an order
 */
function deleteOrder(orderId, data) {
    // Because of it depends on other 'products' in the list if the user can
    // simply delete it, I use a jQuery dialog to give him some options.
    // These options I send to a php script so it knows what should be deleted.
    // This fires when a user clicks on the 'delete' button from a dialog.
    // The dialog uses data to show options but does not change the value of data.
    switch(data.type) {
        case 'A':
            delMsg += '<p>Some message for case A</p>';
            delMsg += '<select>with some options for case A</select>';
            $('#wizard_dialog').append(delMsg);
            $('#wizard_dialog').dialog('option', 'buttons',  [ 
                { text: "Delete", click: function() { 
                        $.post(myUrl, $('#myDeleteOptions').serialize(), function(newData) {
                            // Now the returned data is the updated session data
                            // So I build the orderList again...
                            buildOrderList(newData);
                            ...
                            // Do some other work
                        });
                        $( this ).dialog( "close" );
                        $(this).html(''); }},
                { text: "Cancel", click: function() { $( this ).dialog("close"); $(this).html(''); }}
            ] );
            break;
        case 'B':
            // Do the same thing but different text and <select> elements
            break;
    }
}

orderList正确更新,但是如果我尝试删除另一个订单,jQuery对话框会为我提供当前(正确的产品)选项和以前拥有当前ID的产品的选项。 (希望我试图解释这个问题时没有让任何人失望)

主要问题是如何“刷新”发送到buildOrderList的数据。

由于我在一个新的$ .post中调用该函数并返回了新的数据对象,它应该可以工作,不应该吗?

/**
 * Enable the JQuery dialog
 * (#wizard_dialog)
     * this is the init (note that I only open the dialog in deleteOrder() and set text and buttons according to the data send to deleteOrder() )
 */
$('#wizard_dialog').dialog({
    autoOpen: false,
    resizable: false,
    modal: true,
    dialogClass: "no-close",
    open: function() {
        $('.ui-dialog-buttonpane').find('button:contains("Annuleren")').addClass('cancelButtonClass');
        $('.ui-dialog-buttonpane').find('button:contains("Verwijderen")').addClass('deleteButtonClass');
        $('.ui-dialog :button').blur(); // Because it is dangerous to put focus on 'OK' button
        $('.ui-widget-overlay').css('position', 'fixed'); // Fixing overlay (else in wrong position?)
        if ($(document).height() > $(window).height()) {
            var scrollTop = ($('html').scrollTop()) ? $('html').scrollTop() : $('body').scrollTop(); // Works for Chrome, Firefox, IE...
            $('html').addClass('noscroll').css('top',-scrollTop); // Prevent scroll without hiding the bar (thus preventing page to shift)
        }
    },
    close: function() {
        $('.ui-widget-overlay').css('position', 'absolute'); // Brake overlay again
        var scrollTop = parseInt($('html').css('top'));
        $('html').removeClass('noscroll'); // Allow scrolling again
        $('html,body').scrollTop(-scrollTop);
        $('#wizard_dialog').html('');
    }
});

修改 因为问题可能在对话框中我添加了一些代码。 在第一个代码块中,我更改了deleteOrder();

ANSWER 解决方案相当简单。在添加新的处理程序之前,我忘了关闭点击处理程序。这返回了上一个事件和新事件。

$(document).off('click', '[id^=delete_]').on('click', '[id^=delete_]', function() {
        // Get the orderId from the delete button
        var orderId = $(this).attr('id').split('_');
        orderId = orderId['1'];
        // I call the delete function
        deleteOrder(orderId, data);
    });

0 个答案:

没有答案