Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/385.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 使用jquery和json进行实时搜索_Javascript_Jquery - Fatal编程技术网

Javascript 使用jquery和json进行实时搜索

Javascript 使用jquery和json进行实时搜索,javascript,jquery,Javascript,Jquery,我想做一个实时搜索,然后选择这些建议进一步实施!但它不会在选择字段中输入值。换句话说,选项标记不起作用!!! 这是代码 <div class="container"> <h4 class="publish-work" >PUBLISHED WORKS AVAILABLE</h4> <br> <input type="search" class="search" name="search" > <br&

我想做一个实时搜索,然后选择这些建议进一步实施!但它不会在选择字段中输入值。换句话说,选项标记不起作用!!! 这是代码

   <div class="container">
  <h4 class="publish-work" >PUBLISHED WORKS AVAILABLE</h4>
  <br>
    <input type="search" class="search" name="search" >
    <br>
    <br>
    <table id="datalist" class="table table-bordered" style="height: 400px;    overflow-y: auto;">
       <thead>

         <th>S.no</th>
         <th>Name</th>
         <th>Price</th>
       </thead>
       <tbody>
       </tbody>
    </table>
//index.php

<html lang="en"><head>
    <meta charset="utf-8">

    <title>Live Search</title>

</head>
<body>

<div id="searcharea">

    <label for="search">live search</label>
    <p>Enter the name</p>
    <input type="search" name="search" id="search" placeholder="name or info">

</div>
<div>
    <div id="top"></div>
    <div id="center"></div>
    <div id="bottom"></div>
</div>
<script src="jquery-1.12.2.min.js"></script>
<script src="basic.js"></script>

</body>
</html>
   <div class="container">
  <h4 class="publish-work" >PUBLISHED WORKS AVAILABLE</h4>
  <br>
    <input type="search" class="search" name="search" >
    <br>
    <br>
    <table id="datalist" class="table table-bordered" style="height: 400px;    overflow-y: auto;">
       <thead>

         <th>S.no</th>
         <th>Name</th>
         <th>Price</th>
       </thead>
       <tbody>
       </tbody>
    </table>
//basic.js

$('#search').keyup(function()
{
    var searchField=$('#search').val();
    var myExp=new RegExp(searchField,"i");
    var slct_start='<select>';
    $('#top').html(slct_start);
    $.getJSON('location.json',function(data)
    {   
        var output='<ul class="searchresults">';    

        $.each(data,function(key,val){

            if(val.name.search(myExp)!=-1)
            {
             output='<option '+'value='+val.name+'>'+val.name+'</option>';

            }
        });
            $('#center').html(output);
    });//get json

    var slct_end='</select>';
    $('#bottom').html(slct_end);
});
   <div class="container">
  <h4 class="publish-work" >PUBLISHED WORKS AVAILABLE</h4>
  <br>
    <input type="search" class="search" name="search" >
    <br>
    <br>
    <table id="datalist" class="table table-bordered" style="height: 400px;    overflow-y: auto;">
       <thead>

         <th>S.no</th>
         <th>Name</th>
         <th>Price</th>
       </thead>
       <tbody>
       </tbody>
    </table>

而不是这个键控功能, 您可以使用Jquery自动完成功能来轻松搜索。 试试那个

   <div class="container">
  <h4 class="publish-work" >PUBLISHED WORKS AVAILABLE</h4>
  <br>
    <input type="search" class="search" name="search" >
    <br>
    <br>
    <table id="datalist" class="table table-bordered" style="height: 400px;    overflow-y: auto;">
       <thead>

         <th>S.no</th>
         <th>Name</th>
         <th>Price</th>
       </thead>
       <tbody>
       </tbody>
    </table>

参考此

我发现您的位置。json的数据不标准,它应该是这样的:

 [
  {
    "name":"Barot Bellingham"
    },
  {
    "name":"Jonathan G. Ferrar II"
    },
  {
    "name":"Hillary Hewitt Goldwynn-Post"
    },
  {
    "name":"Hassum Harrod"
    },
  {
    "name":"Jennifer Jerome"
    },
  {
    "name":"LaVonne L. LaRue"
    },
  {
    "name":"Constance Olivia Smith"
    },
  {
    "name":"Riley Rudolph Rewington"
    },
  {
    "name":"Xhou Ta"
    }
]
   <div class="container">
  <h4 class="publish-work" >PUBLISHED WORKS AVAILABLE</h4>
  <br>
    <input type="search" class="search" name="search" >
    <br>
    <br>
    <table id="datalist" class="table table-bordered" style="height: 400px;    overflow-y: auto;">
       <thead>

         <th>S.no</th>
         <th>Name</th>
         <th>Price</th>
       </thead>
       <tbody>
       </tbody>
    </table>

首先,我们将一个html文件设置为index.html

   <div class="container">
  <h4 class="publish-work" >PUBLISHED WORKS AVAILABLE</h4>
  <br>
    <input type="search" class="search" name="search" >
    <br>
    <br>
    <table id="datalist" class="table table-bordered" style="height: 400px;    overflow-y: auto;">
       <thead>

         <th>S.no</th>
         <th>Name</th>
         <th>Price</th>
       </thead>
       <tbody>
       </tbody>
    </table>

   <div class="container">
  <h4 class="publish-work" >PUBLISHED WORKS AVAILABLE</h4>
  <br>
    <input type="search" class="search" name="search" >
    <br>
    <br>
    <table id="datalist" class="table table-bordered" style="height: 400px;    overflow-y: auto;">
       <thead>

         <th>S.no</th>
         <th>Name</th>
         <th>Price</th>
       </thead>
       <tbody>
       </tbody>
    </table>
然后添加一个script.js文件

   <div class="container">
  <h4 class="publish-work" >PUBLISHED WORKS AVAILABLE</h4>
  <br>
    <input type="search" class="search" name="search" >
    <br>
    <br>
    <table id="datalist" class="table table-bordered" style="height: 400px;    overflow-y: auto;">
       <thead>

         <th>S.no</th>
         <th>Name</th>
         <th>Price</th>
       </thead>
       <tbody>
       </tbody>
    </table>
$document.readyfunction {

   <div class="container">
  <h4 class="publish-work" >PUBLISHED WORKS AVAILABLE</h4>
  <br>
    <input type="search" class="search" name="search" >
    <br>
    <br>
    <table id="datalist" class="table table-bordered" style="height: 400px;    overflow-y: auto;">
       <thead>

         <th>S.no</th>
         <th>Name</th>
         <th>Price</th>
       </thead>
       <tbody>
       </tbody>
    </table>
})

   <div class="container">
  <h4 class="publish-work" >PUBLISHED WORKS AVAILABLE</h4>
  <br>
    <input type="search" class="search" name="search" >
    <br>
    <br>
    <table id="datalist" class="table table-bordered" style="height: 400px;    overflow-y: auto;">
       <thead>

         <th>S.no</th>
         <th>Name</th>
         <th>Price</th>
       </thead>
       <tbody>
       </tbody>
    </table>

就是这样。

我喜欢这个。在每次击键时加载JSON无论如何都是糟糕的编程!如何将external location.json文件中的jason数据转换为javascript???在保存Jquery函数的同一页面中声明json数据,而不是通过服务器更新location.json的其他文件。如何将json对象分配给javascript变量,如:var obj=[{name:b},{name:c},{name:d},{name:e},{name:f}]。
   <div class="container">
  <h4 class="publish-work" >PUBLISHED WORKS AVAILABLE</h4>
  <br>
    <input type="search" class="search" name="search" >
    <br>
    <br>
    <table id="datalist" class="table table-bordered" style="height: 400px;    overflow-y: auto;">
       <thead>

         <th>S.no</th>
         <th>Name</th>
         <th>Price</th>
       </thead>
       <tbody>
       </tbody>
    </table>