简体   繁体   English

jQuery在页面之间传递数据

[英]jQuery passing data between pages

I am new to jQuery. 我是jQuery的新手。 Is there any way to retrieve the value of booked in another page through jQuery? 有没有办法通过jQuery检索在另一个页面中booked的值?

$(document).ready(function() {
    $(".active").click(function() {
        var booked=$(this).val();
        confirm(booked);
    });
});

Use cookies or HTML5 localStorage if its purely on the client-side. 如果纯粹在客户端使用cookie或HTML5 localStorage

localStorage.setItem('bookedStatus' + customerId, true);

Else use ajax if the data has already been submitted to server. 如果数据已经提交给服务器,则使用ajax

$.get('/site/getBookingStatus?customerId=' + customerId, function(data){
   alert(data);
});

Alternatively, if this is a simple string you can append with the URL of the page while navigating to another page. 或者,如果这是一个简单的字符串,您可以在导航到另一个页面时附加页面的URL。 If this is secured data, you can encrypt the string and attach. 如果这是安全数据,您可以加密字符串并附加。

Your URL will be : example.com/nextPage?x=booked 您的网址为: example.com/nextPage?x=booked

In the next page you can get the string by decoding it as given : 在下一页中,您可以通过解码它来获取字符串:

var encodedData = window.location.href.split('=')[1];
var bookedValue = decodeURI(encodedData);

If you have encrypted the script, you have to decrypt in the next page. 如果您已加密脚本,则必须在下一页中解密。

localStorage doesn't work well on mobile browsers. localStorage在移动浏览器上不能很好地工作。 I've given up on trying to get localStorage to work on iPhone/safari. 我已经放弃了尝试让localStorage在iPhone / Safari上工作。 If you're not passing too much data then a simple solution is to attach the data to the url you are navigating to, using the usual ?param= syntax: 如果您没有传递太多数据,那么一个简单的解决方案是使用通常的?param =语法将数据附加到您导航到的URL:

// set your data in the source page 
function set_url_data(go_to_url, data) {
  new_url = go_to_url + '?data=' + data;
  window.location.href = new_url;
}

// parse your data in the destination page 
function grab_data_from_url() {
  url = window.location.href;
  data = url.split('data=').pop();
  return(data)
}

You could try cookies if it is on the same domain. 如果cookie位于同一个域中,您可以尝试使用cookie。

You'll need to use the jQuery cookie plugin (to iron out cross browser issues). 您需要使用jQuery cookie 插件 (以解决跨浏览器问题)。

You should try to do something like this: 你应该尝试做这样的事情:

  1. Generate the variable on page 1. 生成第1页的变量。
  2. Save this variable as a session cookie. 将此变量另存为会话cookie。
  3. On page 2, you can now access the session cookie. 在第2页上,您现在可以访问会话cookie。
  4. If the user directly visited page 2 without visiting page 1 you should have a default value setup. 如果用户在未访问第1页的情况下直接访问了第2页,则应设置默认值。
  5. Done! 完成!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM