1

<input>ユーザーがボックス内のテキストを選択しているかどうかを検出するにはどうすればよいですか?

$('input[name=inputname]').keyup(function(e)
{
   if((e.keyCode!=37)&&(e.keyCode!=38)&&(e.keyCode!=39)&&(e.keyCode!=40)) // Check whether the user is navigating inside the box by pressing the arrows
   {
        call_the_function();
   }
});

ユーザーが Shift + 左矢印または Shift + 右矢印を使用して入力したテキストを選択すると、keyUp()関数が実行されます。どうすればこれを防ぐことができますか?

4

3 に答える 3

1

これを試して:

$('input[name=inputname]').keyup(function(e)
{
   if((e.keyCode!=16)&&(e.keyCode!=37)
       ||(e.keyCode!=16)&&(e.keyCode!=38)
       ||(e.keyCode!=16)&&(e.keyCode!=39)
       ||(e.keyCode!=16)&&(e.keyCode!=40)) // Check whether the user is navigating inside the box by pressing the arrows
   {
        call_the_function();
   }
});

これにより、SHIFT キーと、左、右、上、または下の矢印がチェックされます。

于 2013-09-30T10:41:23.883 に答える
0

jquery select() を使用するのはどうですか

http://jsfiddle.net/kasperfish/xWEPh/

//http://api.jquery.com/select/
$( "#target" ).select(function() {
alert( "Handler for .select() called." );
});

キーボードで選択するキーアップ イベントを簡単に無効にすることができます。これは、keyup イベント ハンドラーで行う必要があります。

于 2013-09-30T10:36:53.340 に答える
0

デモ

$('input[name=inputname]').keyup(function (e) {
    if (event.shiftKey && ((e.keyCode >= 37) && (e.keyCode <= 40)))
    {
        return false;
    }else{
        console.log('hi'); //call function when shift+arrow keys not pressed together 
    }
});

また

デモ

$('input[name=inputname]').keyup(function (e) {
    if (!(event.shiftKey && ((e.keyCode >= 37) && (e.keyCode <= 40)))) {
        console.log('hi');  //call function when shift+arrow keys not pressed together 
    }
});
于 2013-09-30T10:40:55.427 に答える