在事件之间传递变量

时间:2012-06-01 18:47:51

标签: javascript jquery

我有一个编辑按钮,点击后会触发下面的第一个事件。它(编辑表单)也有一个取消链接,当点击它时,触发下面的第二个事件。如何确保在取消事件中可以访问PrevContent值?用户可以一次点击任意数量的相应编辑链接。

$('.js-profile-edit').live('click',function(){

    var TrBlock     = $(this).closest('tr');
    var PrevContent = TrBlock.html();
    var colspan     = TrBlock.parent().prev().find('a:first').first().data('span');

    $.ajax({
        url     : $(this).attr('href'),
        type    : 'GET',
        success : function(response){
            TrBlock.html('<td colspan="'+colspan+'">'+response+'</td>');
        },
        error    : function(jqXHR, textStatus, errorThrown){
            uiAlert({message : 'Something Broke. Try again later'})
        }
    });
    return false;
});


$('.js-profile-edit-cancel').live('click',function(){
    $(this).closest('tr').html(PrevContent);
    return false;
});

2 个答案:

答案 0 :(得分:1)

也许您可以将PrevContent填入取消按钮的data object

写入js-profile-edit点击处理程序:

$('.js-profile-edit-cancel').data('PrevContent', PrevContent);

并阅读js-profile-edit-cancel点击处理程序:

var PrevContent = $(this).data('PrevContent');

答案 1 :(得分:0)

您应该能够将prevContent .data()存储在最近的<tr>上:

$('.js-profile-edit').live('click',function(){

    var TrBlock     = $(this).closest('tr');
    var PrevContent = TrBlock.html();
    TrBlock.data({contents: PrevContent});  // save it
    ...
})

$('.js-profile-edit-cancel').live('click',function() {
    var TrBlock = $(this).closest('tr');
    TrBlock.html(TrBlock.data('contents'));
    return false;
});
相关问题