2014-03-25 63 views
0

我有一個工作聯編輯腳本,它可以讓用戶編輯他或她的名字。jQuery的內聯編輯返回鍵

但它目前並不「保存」,當用戶點擊enter key

腳本:

<span class="pageTitle" id="username">Visitor 123123981203980912 <span class="iconb" data-icon=""></span></span> 

    // Inline edit 
    $.fn.inlineEdit = function(replaceWith, connectWith) { 

     $(this).hover(function() { 
      $(this).addClass('hover'); 
     }, function() { 
      $(this).removeClass('hover'); 
     }); 

     $(this).click(function() { 

      var elem = $(this); 

      elem.hide(); 
      elem.after(replaceWith); 
      replaceWith.focus(); 

      replaceWith.blur(function() { 

       if ($(this).val() != "") { 
        connectWith.val($(this).val()).change(); 
        elem.html($(this).val() + ' <span class="iconb" data-icon=""></span>'); 
       } 

       $(this).remove(); 
       elem.show(); 
      }); 
     }); 
    }; 

    var replaceWith = $('<input name="temp" type="text" class="inlineEdit" />'), 
     connectWith = $('input[name="hiddenField"]'); 

    $('#username').inlineEdit(replaceWith, connectWith);    

我怎樣才能使上面還反應時enter key被擊中?

回答

1

您需要檢測輸入新聞並在模糊功能中做同樣的事情。將以下內容添加到您的js。 Demo

replaceWith.keypress(function(e) { 
     if(e.which == 13) { 
      if ($(this).val() != "") { 
       connectWith.val($(this).val()).change(); 
       elem.html($(this).val() + ' <span class="iconb" data-icon=""></span>'); 
      } 

      $(this).remove(); 
      elem.show(); 
     } 
    }); 
0

當回車鍵這應該火被壓在裏面輸入:

$('#username').live("keypress", function(e) { 
     if (e.keyCode == 13) { 
      // action here 
     } 
}); 
0

你可以調用的keydown相同的功能,檢查e.keyCode的回車鍵是13..a小提琴會有用..

還要檢查這個link

感謝, Hardik