weixin_33721427 2016-02-28 08:11 采纳率: 0%
浏览 52

搜索框结果== Json结果

I have a Node.js server that reads a JSON file and there are a lot of information in it. One of them is ID of a person, I have a search box in my HTML page I wanna right a jquery method(ajax required) for the client side to find if the ID that the user put in the search box is the same as the ID in JSON, if it is I wanna return some information about that person, let's say name and username of the user.

Note: I am 100% sure that my server.js file works.

Please help me, I am kind of a newbie in Jquery.

I have so far;

$(document).ready(function(){
    $("#id_of_the_search_box_button").click(function (any_function_variable_name) {
    $.ajax({
      type: 'GET',
      url: '/url_of_the_related_server_side',
      dataType: 'json'
      })
          .done(function(data){
              console.log(data);
              alert(JSON.stringify(data));

              data.forEach(function (any_function_variable_name) {

                if(any_function_variable_name.search_box_ID == any_function_variable_name.name_in_the_JSON_file_for_one_of_the_searched_ID){

                    $userElement = $("<li>").html("<strong>ID: </strong>" + any_function_variable_name.id); //id is defined in JSON and I was able to use in another part of the jquery
                    $userBlock = $("<ul>")
                        .append(
                            $("<li>").html("<strong>Name: </strong>" + any_function_variable_name.name) //name of the user
                        )
                        .append(
                            $userElement
                        )
                        .append(
                            $("<li>").html("<strong>User User Name: </strong>" + any_function_variable_name.userName)
                        );
                                                                  $any_function_variable_nameBlock.insertAfter("#search");
            }
            });
    });})}) 

Note: I don't know if I could close the brackets right.

Thanks in advance!

  • 写回答

1条回答 默认 最新

  • Didn"t forge 2016-02-28 08:59
    关注

    I guess you are looking for something like as shown below ...

    Instead of making an ajax call, I wrote with static data as below ...

    HTML

    <input id="searchBox" type="text" />
    <button id="id_of_the_search_box_button">
    Search
    </button>
    
    <ul>
    
    </ul>
    

    JS

    var data = [
      {
        "id": 37595960858,
        "in_reply_to_screen_name": null,
        "user": {
          "id": 2384451,
          "id_str": "238678",
          "name": "Doga I",
          "screen_name": "IDoga"
        }
      },
      {
        "id": 65739068412,
        "in_reply_to_screen_name": null,
        "user": {
          "id": 2878009,
          "id_str": "235678",
          "name": "Jon Doe",
          "user_name": "JD"
        }
      }
    ];
    
    
    $(document).ready(function(){
      $("#id_of_the_search_box_button").click(function (any_function_variable_name) {
    
        var searchText = parseInt($('#searchBox').val());
    
    
        data.forEach(function (any_function_variable_name) {
    
          if(any_function_variable_name.id === searchText){
    
            $("ul")
                .append('<li> <strong>ID: </strong>' + any_function_variable_name.user.id)
                .append('<li> <strong>User Name: </strong>' + any_function_variable_name.user.user_name)
                .append('<li> <strong>Name: </strong>' + any_function_variable_name.user.name)
                .append('<li> <strong>Screen Name: </strong>' + any_function_variable_name.user.screen_name);
    
          } //End if
    
        }); //End forEach
    
      });
    }); 
    

    which makes your ajax call like this ...

    $(document).ready(function(){
      $("#id_of_the_search_box_button").click(function (any_function_variable_name) {
        $.ajax({
          type: 'GET',
          url: '/url_of_the_related_server_side',
          dataType: 'json'
        })
        .done(function(data){
    
    
          var searchText = parseInt($('#searchBox').val());
    
    
          data.forEach(function (any_function_variable_name) {
    
            if(any_function_variable_name.id === searchText){
    
              $("ul")
                .append('<li> <strong>ID: </strong>' + any_function_variable_name.user.id)
                .append('<li> <strong>User Name: </strong>' + any_function_variable_name.user.user_name)
                .append('<li> <strong>Name: </strong>' + any_function_variable_name.user.name)
                .append('<li> <strong>Screen Name: </strong>' + any_function_variable_name.user.screen_name);
    
            } //End if
    
          }); //End forEach
    
    
    
        });
      });
    });
    

    Following is the fiddler

    https://jsfiddle.net/rrtbz4bo/7/

    Enter 37595960858 in search box and hit search.

    UPDATE: call the /find/id?id=... api directly if it returns data like

    {
        "id": 37595960858,
        "in_reply_to_screen_name": null,
        "user": {
          "id": 2384451,
          "id_str": "238678",
          "name": "Doga I",
          "screen_name": "IDoga"
        }
      }
    

    Then following should work.

    $(document).ready(function(){
      $("#id_of_the_search_box_button").click(function (any_function_variable_name) {
        $.ajax({
          type: 'GET',
          //Or whatever your URL structure is
          url: 'http://127.0.0.1:5230/find/id?Id=' + $('#searchBox').val(),
          dataType: 'json'
        })
        .done(function(data){
          $("ul")
          .append('<li> <strong>ID: </strong>' + data.user.id)
          .append('<li> <strong>User Name: </strong>' + data.user.user_name)
          .append('<li> <strong>Name: </strong>' + data.user.name)
          .append('<li> <strong>Screen Name: </strong>' + data.user.screen_name);
        });
      });
    });
    

    If this doesn't work, post a fiddler and i'll try and make it work.

    Hope this helps ...

    评论

报告相同问题?

悬赏问题

  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的
  • ¥15 r语言蛋白组学相关问题