当输入参数长度=8时开始搜索


Begin search when input parameter length =8

我使用两个文件进行搜索。index.html生成一个表单来调用PHP搜索函数(ss.php

index.html代码:

<form action="ss.php" method="get">
     <input name="q" type="text"> 
     <input type="submit"> 
</form>

和ss.php代码(php搜索功能):

<?php
  $dir = 'ups';
  $exclude = array('.','..','.htaccess');
  $q = (isset($_GET['q']))? strtolower($_GET['q']) : '';
  $res = opendir($dir);
  while(false!== ($file = readdir($res))) {
    if(strpos(strtolower($file),$q)!== false &&!in_array($file,$exclude)) {
      echo "<a href='$dir/$file' target = '_blank'>$file</a>";
      echo "<br>";
    }
  }
closedir($res);
?>

我希望在输入参数的长度为八位数时开始搜索。

编辑:

感谢每一个人已解决我用了这个代码:

<form action="ss.php" method="get"><input name="q"
type="text" pattern=".{8,10}" title="8 to 10 characters" maxlength="10">
<input type="submit"></form>
$(textbox).keypress(function() {
    if(this.length >7 {   
       //ajax call
    }
});

这是您需要的格式

最简单的方法是在用户键入8个字符时附加提交事件。

试试这个onkeyup="…"事件:

<input name="q" type="text" onkeyup="if (this.value.length >= 8) { document.forms[0].submit() }">

编辑:

以上不允许超过8个字符。如果你使用的是jquery,试试这个:

onkeyup="if (this.value.length >= 8) { $.post('/form-action', { q: this.value }, function(response){ alert(response); }); }"

html:

<form id='form' action="ss.php" method="get">
    <input id='input' name="q" type="text"/> 
     <input type="submit"/> 
</form>  

javascript:

var form = document.getElementById('form');
var input = document.getElementById('input');
input.onkeypress = function(){
    if(input.value.length >= 8){
        form.submit();
    }
}