如何根据按键更改数据

时间:2013-08-01 20:23:49

标签: php javascript jquery ajax

我想在页面上显示数组中的值,并给出一个输入框以将关键字作为输入,并根据过滤,我想更改内容......

这是代码

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">

<head>
        <meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
        <meta name="author" content="Ahmet YUCEL" />
    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
    <script src="//ajax.googleapis.com/ajax/libs/jqueryui/1.10.3/jquery-ui.min.js"></script>
<title>Untitled 2</title>
</head>

<body>

<input type="text" id="input_element_id" name="input_element_id"/>
<?php
$all_elms=array("439399343"=>"Anshul Singhal","3439493943"=>"Mayank","343949398438"=>"ankit","34839849839"=>"shivank","3843884384"=>"dheeraj","383849839"=>"sparrow","8348399219"=>"makar makru","43891928382"=>"lying dehhhee");
echo json_encode($all_elms);
?>

<div id="user"></div>

</body>

<script type="text/javascript">
$('#input_element_id').keydown(function() {
        val = $(this).val();
        $(all_elms).each(function(){
                if(this.search(val) >= -1) $("#user-" + this).show();  
        });    
});
</script>
</html>

1 个答案:

答案 0 :(得分:0)

尝试以下操作:以“li”格式回显列表,然后将代码下方的代码粘贴到编辑器

// on keydown on text box
  $txtInput.on("keyup", function (e) {                   
     var txt = $(this).val().toLowerCase();

       //for backspace
          if (e.which == 8) {
             if (txt.length == 0) {
                  renderList(arrText);
             }
          }

       if (txt.length >= 1) {
         var filterList = searchInList(arrText, txt);
         if (filterList.length > 0) {
              renderList(filterList);
          } else {
             console.log("Not Found");
             renderList(arrText);
          }
      }                     
  });

请在此处查看完整示例:http://jsfiddle.net/m25UW/

我希望它有所帮助。