2011-06-18 39 views
0

我有一個文本列表。每當這個文本被點擊時,它變成了一個文本框。但是,如果我雙擊文本框就會變得不可用。這是我的javascript:動態文本框列表bug jquery

$('.l').live('click', function() { 
$('<input type="text" id="ltb" name="'+$(this).attr("id")+'" class="'+$(this).attr("class")+'">').insertAfter($(this)).val($(this).text()); 
$('#ltb').focus().blur(function() { 
    $('<div id="'+$(this).attr("name")+'" class="l">'+$(this).val()+'</div>').insertAfter($(this)); 
    $('#'+$(this).attr("class")).text($(this).val()); 
    $(this).remove(); 
}); 
$('#ltb').keypress(function(event) 
{ 
    var keycode = (event.keyCode ? event.keyCode : event.which); 
    if(keycode == '13'){ 
     $('<div id="'+$(this).attr("name")+'" class="l">'+$(this).val()+'</div>').insertAfter($(this)); 
     $('#'+$(this).attr("class")).text($(this).val()); 
     $(this).remove(); 
    } 

}); 
$(this).remove(); 
//$(this).hide(); 
}); 

其中包含文本的DIV看起來是這樣的:

<div id="xxxxxxx" class="l">Text here</div> 

回答

0

修正了它。

$('.l').live('click', function() { 
    if ($('#ltb').length > 0) return; 
    // rest of code here 

如果用one替換live還將工作。我認爲問題在於創建兩個具有相同id的div,因此在創建一個之前檢查它是否存在。

here

與多於一個的元件 一個文件使用相同的ID無效。

,更多的是one(用於綁定的事件一次,發現了這個前幾天讀一個jQuery袖珍參考):

http://api.jquery.com/one/

+0

謝謝。這工作完美。 – jbills

0

變化divz-index參數。

+0

這並沒有幫助。我做了一些測試,發現點擊它然後再次點擊會造成這個錯誤。 – jbills