2016-11-14 36 views
0

點擊時將跨度轉換爲輸入,當點擊它後再次變爲跨度。但它只轉換一次,然後不再發生點擊後將跨度轉換爲輸入

這是代碼。

$(function() { 
    $(document).on('click', 'span.loadNum', function() { 
     var input = $('<input />', { 
      'type': 'text', 
       'name': 'unique', 
       'value': $(this).html() 
     }); 
     $(this).parent().append(input); 
     $(this).remove(); 
     input.focus(); 
    }); 

    $(document).on('blur', 'input', function() { 
     $(this).parent().append($('<span />').html($(this).val())); 
     $(this).remove(); 
    }); 
}); 

,並跨越

<span class="loadNum">Hasha</span> 
<span class="loadNum">dbhfb</span> 

而且誰能告訴如何實現這一點,當我按下轉換的輸入標籤輸入它應該提交一個Ajax調用。

+0

http://jsfiddle.net/hW3vk/114/ – Alexis

回答

1

當您重新添加span時,它沒有類。所以span.loadNum選擇器不會識別它。 Add the class

$(this).parent().append($('<span />').addClass('loadNum').html($(this).val())); 
+0

還有一點故障......跨度正在改變他們的地方!那看起來不錯! – Alexis

+0

@Alexis:我想你需要稍微修改一下你的邏輯。這解決了您在原始問題中遇到的問題,但我並沒有承諾您的整個應用程序是正確的或理想實施的。這個答案不會引入你在該評論中描述的問題。 – David

+0

好吧!但是還有另外一個問題。Ajax提交關鍵'13'鍵 – Alexis