Javascript JS:如何将所选数据传递到下一页?
我的用户有一个sharepoint列表,可以在其中添加项目。有时他们必须创建非常相似的项目。因此,我想在列表中添加一列,其中有一个按钮,或者将其中一列添加到链接中。如果按下此按钮:Javascript JS:如何将所选数据传递到下一页?,javascript,jquery,sharepoint,Javascript,Jquery,Sharepoint,我的用户有一个sharepoint列表,可以在其中添加项目。有时他们必须创建非常相似的项目。因此,我想在列表中添加一列,其中有一个按钮,或者将其中一列添加到链接中。如果按下此按钮: 将存储按钮/链接行中的项目数据 转到新项目创建页面 使用存储的数据自动填充所有输入 这样,用户只需修改几个字段,就可以更快地完成新项目的创建。仅仅从新项目页面访问显示页面是不够的,因为我们至少需要知道项目的ID才能选择正确的项目 最好的方法是什么?非常感谢你的任何想法 您可以使用localstorage api,该a
最好的方法是什么?非常感谢你的任何想法 您可以使用localstorage api,该api可从窗口对象访问 一旦在作用域中有了这些值,在localStorage中设置它们,您就可以在刷新后在新页面上访问该值,等等 例如:
//I'm using JSON.stringify here because it will allow you to store objects, not just primitive values such as Number, String, Boolean.
window.localStorage.setItem(“key”, JSON.stringify({“Foo”:”bar”});
然后在你的新页面上查找
var item = window.localStorage.getItem(“key”);
//We need to parse the JSON we stringified, otherwise you'll end up with a string value.
console.log(JSON.parse(item));
这将记录:
{"Foo": "bar"}
然后,您可以自由地使用从localStorage获取的值检索的字段填充任何表单字段
然后可以在脚本中的逻辑点清理本地存储
window.localStorage.removeItem("key");
现在清楚了
//This will now log undefined.
console.log(JSON.parse(window.localStorage.getItem("key"));
在代码中使用JSON.stringify和JSON.parse调用很容易导致阅读困难。我建议您创建或使用第三方服务与之交互
下面是一个简单的示例,可以对其进行改进,但这绝不是一个完整的、全方位的本地存储包装器
//Create a **single** global App object, you can access the service
//hereon out with 'window.App'.
var App = (window.App || (window.App = {}));
//Make a little service for interacting with local storage
App.Storage = {
//Wrap the call to JSON.parse inside the service's get function
getItem: function (key) {
return JSON.parse(window.localStorage.getItem(key));
},
//Wrap the call to JSON.stringify in the service's set function
setItem: function (key, value) {
return window.localStorage.setItem(key, JSON.stringify(value));
},
//Not really neccesary as no JSON calls are used here
//Worth adding for completeness sakes though.
removeItem: function (key) {
return window.localStorage.removeItem(key);
}
};
如果您使用这样的服务,那么您的代码就会变得更易于阅读
例如:
var foo = { "Foo": "bar" };
//No JSON.stringify call :)
window.App.Storage.setItem("key", foo);
//Logs the object with no JSON.parse call directly in your business logic.
console.log(window.App.Storage.getItem("key"));
//Remove the item...
window.App.Storage.removeItem("key");
//Will now log 'undefined'
console.log(window.App.Storage.getItem("key"));
var Storage = App.Storage;
Storage.getItem();
Storage.setItem();
Storage.removeItem();
注意,在浏览器中暗含“window”,因此您可以将上面的“window.App”替换为“App”
您还可以通过在本地范围内缓存存储服务对象来节省javascript的一些工作
例如:
var foo = { "Foo": "bar" };
//No JSON.stringify call :)
window.App.Storage.setItem("key", foo);
//Logs the object with no JSON.parse call directly in your business logic.
console.log(window.App.Storage.getItem("key"));
//Remove the item...
window.App.Storage.removeItem("key");
//Will now log 'undefined'
console.log(window.App.Storage.getItem("key"));
var Storage = App.Storage;
Storage.getItem();
Storage.setItem();
Storage.removeItem();
非常感谢您的详细指导,它成功了!即使这是我第一次尝试,也很容易理解!没问题,快乐编码!