将多个搜索结果中的一个发送到另一个页面

send one of multiple search result to another page

本文关键字:一个 另一个 搜索结果      更新时间:2023-09-26

我有一个页面userLanding.jsp当用户执行搜索时,页面将给出多个结果。我需要检查选定的动态结果(到目前为止成功(,现在的问题是我需要将所选div的数据发送/传输/检索到另一个页面。

我该怎么做?

这是我遵循的代码,用于检查选择了哪个结果。

$('.demo-card-wide').click(function(){
    article = $(this).text(); 
});
$(document).on('click', '.demo-card-wide', function(){
    alert("clicked on result!!");
    var id = this.id;
    window.location = "http://localhost:8080/CarPool/test.jsp#"+id;  
});

由于您要重定向到一个具有要设置为URL哈希值的新页面,因此您可以使用window.location.hash在页面加载时访问该值(尽管我不相信过去的数据,因为它可以在加载后被页面更改(

$(function(){
    var id = window.location.hash;
    // do something with id...
});

如果需要将数据保留得比这更远,则可以查看localstorage或服务器端解决方案。


只是为了咧嘴笑,这是我的做法(使用 localstorage(:

使此代码可用于两个页面:

  /**
   * Feature detect + local reference for simple use of local storage
   * Use this like:
   * if (storage) {
   *    storage.setItem('key', 'value');
   *    storage.getItem('key');
   * }
   *
   */
  var storage;
  var fail;
  var uid;
  try {
    uid = new Date;
    (storage = window.localStorage).setItem(uid, uid);
    fail = storage.getItem(uid) != uid;
    storage.removeItem(uid);
    fail && (storage = false);
  } catch (exception) {}
  /* end  Feature detect + local reference */

在第一页上有这个:

  $(document).on('click', '.demo-card-wide', function() {
    if (storage) {
      alert("clicked on result!!");
      storage.setItem('article-id', this.id);
      window.location = "http://localhost:8080/CarPool/test.jsp";
    } else // some error message
  });

在第二页上,执行以下操作:

  $(function() {
    if (storage) {
      var id = storage.getItem('article-id');
      // do something with id...
    }
  });