2017-01-17 5 views
0

私は、ユーザーのキーを押して、テキスト入力中にキーを入力した場合にアラートを表示したいのですが、何かが間違っている:入力キー押下機能 - 何か間違っ

<input type='text' id='inputrename' placeholder='type and press Enter'> 

JS

$('#inputrename').keypress(function(event) { 
if (event.keycode === 13) { 
    alert ('323'); 
} 
}); 

アラートが表示されません。

$('#inputrename').keypress(function(event) { 
 
if (event.keycode === 13) { 
 
\t alert ('323'); 
 
} 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<input type='text' class='mdinput' id='inputrename' maxlength='50' placeholder='type and press Enter'>

+0

'keyCode'を試してください。 JavaScriptは大文字と小文字を区別 – putvande

+0

http://stackoverflow.com/a/41261180/4206079 – Banzay

答えて

2

使用keyCode代わりにkeycode

$('#inputrename').keypress(function(event) { 
 
if (event.keyCode === 13) { 
 
\t alert ('323'); 
 
} 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<input type='text' class='mdinput' id='inputrename' maxlength='50' placeholder='type and press Enter'>

+0

Thansk a lot。解決済み。 – bonaca

+0

歓迎です.. –

1

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 

 
<script> 
 
    $(function(){ 
 
    
 
    $('#inputrename').keydown(function(e){ 
 
     
 
     if(e.which === 13) 
 
     alert('Enter key pressed') 
 
     
 
    }); 
 
    
 
    }); 
 
</script> 
 

 
<input type='text' class='mdinput' id='inputrename' maxlength='50' placeholder='type and press Enter'>

1

$('#inputrename').keypress(function(event) { 
 
if (event.keyCode === 13 || event.charCode === 13) { 
 
\t alert ('323'); 
 
} 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<input type='text' class='mdinput' id='inputrename' maxlength='50' placeholder='type and press Enter'>

event.keyCode:は、KeyPressイベントでの非文字キーやキーボードイベントの他のタイプのいずれかのキーのUnicode値を返します。

event.charCode:キー押しイベント中に押された文字キーのUnicode値を返します。

関連する問題