如何在jquery中找到两个keydown事件

时间:2013-06-18 04:18:24

标签: jquery html css

如何查找是否已触发两个keydown事件。我正在创建一个搜索框,在输入两个字母后,结果会在页面中滑动..任何建议

3 个答案:

答案 0 :(得分:1)

你必须在keyup事件中调用一个函数,然后计算该输入框的输入长度。

示例:

<input name="test" id="test" onkeyup="search();"/>

<script>
function search()
{
  var textBox = document.getElementById("test");
  var textLength = textBox.value.length;
  if(textLength > 1)
  {
      // Do your stuff 
  }
}

</script>

明智地使用它应该有效。干杯:)

答案 1 :(得分:1)

你可以算上字符,比如

$("#your_input_id").on("keyup", function() {
   if( $.trim($(this).val().length) >= 2 ) {
       //show suggestions
   }
});

答案 2 :(得分:0)

使用.keydown()可以检查发件人的输入长度(搜索框中的内容)是否大于2.

http://api.jquery.com/keydown/

例如

$('#myTextBox').keydown(function() {
    alert($(this).val().length);
})

然而,听起来您可能会受益于使用jQueryUI和自动完成功能。 http://jqueryui.com/autocomplete/

相关问题