bug^君 2013-03-20 00:02 采纳率: 25%
浏览 16

搜索结果链接

I currently have a job board on my site where users can put in a job title or keyword and a location to search for a job.

The results are displayed using Jquery/AJAX without a page refresh. However, I'd like to be able to link to the results for a specific search. For example, if I search for web development positions in OH, I'd like to be able to send a link to someone else who will be able to click on the link and go directly to the page with those results already loaded.

I have some knowledge of PHP but none of Ajax or Javascript/Jquery, which is why this is so hard for me. I'm hoping someone can help me with this.

Here's the code that is currently displaying the results:

  jQuery(document).ready(function ($) {
      // search button clicked
      jQuery("input[name='indeedsearchbutton']").click(function () {
          indeedsearchbusy();
          indeedsearchcall();
      })

      // reset page if query changes
      jQuery("input[name='q']").change(function () {
          jQuery("input[name='start']").val(1);
      })

      // reset page if location changes
      jQuery("input[name='l']").change(function () {
          jQuery("input[name='start']").val(1);
      })

      if (autosearch) {
          indeedsearchbusy();
          indeedsearchcall();
      }
  })

  // pagination set start position
  function setstart(s) {
      jQuery("input[name='start']").val(s);
      indeedsearchbusy();
      indeedsearchcall();
  }

  // get json data from inputs for ajax call
  function getdata() {
      var d = {
          action: 'indeedcallback',
          q: (jQuery("input[name='q']").length != 0 ? jQuery("input[name='q']").val() : jQuery("select[name='q']").val()),
          l: jQuery("input[name='l']").val(),
          start: jQuery("input[name='start']").val()
      }
      return d;
  }

  // display json results from ajax call
  function indeeddisplayresults(resultsjson) {
      var rpage = "";
      if (resultsjson.pagination) rpage += resultsjson.pagination;

      if (resultsjson.html) rpage += resultsjson.html;

      if (resultsjson.pagination) rpage += resultsjson.pagination;

      jQuery("div#indeedsearchresults").html(rpage);
  }

  // ajax call to search indeed
  function indeedsearchcall() {
      var data = getdata();
      jQuery.post(wpajaxurl, data, function (response) {
          resultjson = eval("(" + response + ")");
          if (resultjson) {
              indeeddisplayresults(resultjson);
          }
      })
  }
  • 写回答

1条回答 默认 最新

  • perhaps? 2013-03-20 00:19
    关注

    I would just use the URL and some sort of hashbang (GETish) functionality. So each time someone does a search, the URL changes, and each time the page is loaded, it looks at the URL for search parameters and if they exist, runs a search...

    There is a nice GIST on this with a full explanation of the code below: https://gist.github.com/miohtama/1570295

    function parseHashBangArgs(aURL) {
    
    aURL = aURL || window.location.href;
    
    var vars = {};
    var hashes = aURL.slice(aURL.indexOf('#') + 1).split('&');
    
    for(var i = 0; i < hashes.length; i++) {
       var hash = hashes[i].split('=');
    
       if(hash.length > 1) {
           vars[hash[0]] = hash[1];
       } else {
          vars[hash[0]] = null;
       }      
    }
    
    return vars;
    

    }

    OK in reference to your question for HOW to use this: I have made a working example (code below), but you will have to implement in your own code (I cant do literally everything for you)...

    <input id="searchBar" type="text" value="enter search here" /><br />
    <input id="theButton" type="button" name="" value="getHash" /><br />
    Hash value is: <a id="hashValue"></a>
    
    jQuery(document).ready(function ($) {
        $('#searchBar').change(function() {
            search(document.getElementById('searchBar').value)
        });
        $('#theButton').click(function() {
            getHash();
        });
    })
    function search(searchTerms){
        location.hash = "searchTerm="+searchTerms;   
    }
    
    function getHash(){
        var hashObj= new Object(); 
        hashObj=(parseHashBangArgs())
        document.getElementById('hashValue').innerHTML=hashObj.searchTerm;
    }
    
    function parseHashBangArgs(aURL) {
    ... code from GIST above... 
    
    评论

报告相同问题?

悬赏问题

  • ¥20 delta降尺度方法,未来数据怎么降尺度
  • ¥15 c# 使用NPOI快速将datatable数据导入excel中指定sheet,要求快速高效
  • ¥15 再不同版本的系统上,TCP传输速度不一致
  • ¥15 高德地图点聚合中Marker的位置无法实时更新
  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程