所以我還沒輸入:的jQuery的keydown /按鍵不工作的權利
<input type="text" name="in" />
,我需要獲得輸入的電流value
,因爲我打字,我面臨的問題是,如果我鍵入pass
和我做了一個console.log我將得到將是pas
錯過了最後一個字符;但我需要整個字符串pass
你可以在這裏看到一個現場演示:http://jsfiddle.net/HjVHV/1/有什麼問題?
所以我還沒輸入:的jQuery的keydown /按鍵不工作的權利
<input type="text" name="in" />
,我需要獲得輸入的電流value
,因爲我打字,我面臨的問題是,如果我鍵入pass
和我做了一個console.log我將得到將是pas
錯過了最後一個字符;但我需要整個字符串pass
你可以在這裏看到一個現場演示:http://jsfiddle.net/HjVHV/1/有什麼問題?
您應該使用keyup
事件:
$('input[name=in]').keyup(function()
{
// $(this).val()
});
裏面你得到你剛剛進入
值你應該使用keyup
:
$('input').keyup(function(){...
你可以跟上使用.keydown()
如果你想,只需追加使用.fromCharCode()
按下的字符,如下所示:
$('input').keydown(function(e){
$('.output').html($('input').val() + String.fromCharCode(e.which).toLowerCase());
});
見工作
您應該使用的關鍵事件的方式。
這是基於你的JS提琴
$('input').keyup(function(){
$('.output').html($('input').val());
});
示例代碼這是你在找什麼?
我複製並從http://forum.jquery.com/topic/can-change-handle-immediate-text-changes#14737000000967875
$('#input').each(function() {
// Save current value of element
$(this).data('oldVal', $(this));
// Look for changes in the value
$(this).bind("propertychange keyup input paste", function(event){
// If value has changed...
if ($(this).data('oldVal') != $(this).val()) {
// Updated stored value
$(this).data('oldVal', $(this).val());
// Do action
$('.output').html($('#input').val());
}
});
});
改性下面的代碼塊