Domanda

I have this jquery and form and I want to be able to exclude certain keystrokes from triggering the search action. Is there an easy way to just allow e.g. alpha numeric keystrokes? So I can disregard, ctrl, shift keys etc...

$(function() {
  var $searchField;
  return $searchField = $('.search');
});

$searchField.focus().val($searchField.val());

$(document).on("keyup", ".search", function(e) {
  return delay(function() {
  $('.search').attr('readonly', 'readonly');
  return $('.search').parent('form').submit();
  }, 500);
});

<form accept-charset="UTF-8" action="/" method="get">
  <input class="search" focus="true" id="search" name="search" placeholder="Search" type="text" value="">
 </form>
È stato utile?

Soluzione

$(document).on("keyup", ".search", function (e) {
    if (e.keyCode == 16 || e.keyCode ==17) { //shift or ctrl
        //Do nothing 
    } else {
        return delay(function () {
            $('.search').attr('readonly', 'readonly');
            return $('.search').parent('form').submit();
        }, 500);
    }
});

DEMO

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top