2010-02-11 32 views
0

我希望這樣設置,以便在單擊某個文本主體時彈出一個文本輸入字段,並且用戶可以將數據輸入到字段中,然後單擊「保存」或「取消」按鈕,分別將其發送到數據庫或重置值。但是,儘管使用了「返回false」,我似乎無法保持這些代碼不被重複激活。我無法'返回false'來讓.live()不會重複傳播代碼

我已經使用了die()並且可以工作,但是我也希望能夠在用戶保存或取消之後重新實現原始的單擊事件,並且無法想出實現該功能的方法。先謝謝您的幫助。

$('td[name]').live("click", function() { 
    nameof = $(this).attr("name"); 
    idof = $(this).attr("id"); 
    valueof = $(this).html(); 

    $(this).html('<input type="text" name="' + nameof + '" value="' + valueof + '"><input type="hidden" name="id" value="' + idof + '"><span id="save">save</span> &nbsp <span id="cancel">cancel</span>'); 

    return false; 
}); 

更新:

這裏就是我終於想出了(包括所有編輯就地代碼):

$('td[name="grouping"] span, td[name="title"] span').live('click', function() {   
    nameof = $(this).parent().attr("name"); 
    idof = $(this).parent().attr("id"); 
    valueof = $(this).html(); 

    if($("table td>span").children('input').length > 0) {} 
    else { 
     if($(this).children().length > 0) {} 
     else { 
      $(this).html('<input type="text" name="' + nameof + '" value="' + valueof + '"><input type="hidden" name="id" value="' + idof + '"><input type="hidden" name="originalinput" value="' + valueof + '"><span class="save">save</span> &nbsp; <span class="cancel">cancel</span>'); 
     } 
    } 

}); 

$('.cancel').live('click', function() {   
    $(this).parent().html($(this).siblings('input[name="originalinput"]').attr("value")); 
}); 

$('.save').live('click', function() { 

    savevalue = $(this).siblings('input[type="text"]').attr("value"); 
    saveid = $(this).siblings('input[name="id"]').attr("value"); 
    savecolumn = $(this).siblings('input[type="text"]').attr("name"); 

    $.ajax({ 
     type: "POST", 
     url: "../php/adminajax.php", 
     data: 'id=' + saveid + '&' + savecolumn + '=' + savevalue 
    }); 

    $(this).parent().html(savevalue); 
}); 

$('#saving').ajaxStart(function() { 
    $(this).fadeIn(100); 
}); 

$('#saving').ajaxStop(function() { 
    $(this).fadeOut(2000); 
}); 

我敢肯定,這多少混亂比任何我所能」已經下載了,但至少我現在知道AJAX就地編輯的基礎知識。感謝所有人的幫助。

+0

有點讓我困惑。你希望點擊事件激活一次或者當點擊並彈出文本框時不能再次點擊? – ntan 2010-02-11 13:00:19

+0

我很確定你不能用.live方法做到這一點!嘗試使用.click,雖然速度較慢。 – stoimen 2010-02-11 13:07:36

+0

@stoimen - 爲什麼'click'會變慢?如果有任何事情會更快,因爲事件在處理之前不需要通過DOM冒泡。 – tvanfosson 2010-02-11 13:13:48

回答

2

看起來你基本上試圖做一個現場編輯交易。您遇到的問題是用戶的點擊正在通過文本框傳播/跨越到其父容器,從而再次觸發點擊事件。您需要對其進行過濾,以便它不會選擇在其內容中包含輸入的td。沿此線的東西:

$('td[name]:not(td>input)').live(...); 

雖然沒有直接回答你在找什麼,爲什麼不使用現有的插件,像一個found here

+0

如果我只是這樣做,我不知道我現在知道什麼;) – dclowd9901 2010-02-11 13:25:08

+0

試過這個,並沒有工作,直到我指定'td [name]:not(td:has(input))'但我決定,相反,只需要使用.once()命令,這似乎正是我想要的。 – dclowd9901 2010-02-11 15:02:17