- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我是一名学生,我正在为一门类(class)建立一个网站。这是网站中的js、html、css页面。当我运行这个程序时,它会在本地存储中保存街道地址,但不会保存城市和邮政编码。我不明白为什么会这样。 session 存储中根本没有显示任何内容。我附上了 app.js、html-template.js、menu.js、app.css 和 submit.html。我附上了浏览器调试器的屏幕截图,显示了本地存储中的内容。
**APP.JS**
var App = {
Menu: null,
OrderCostTotal: 0,
PendingOrderKey: "PendingOrder",
UsedAddressesKey: "UsedAddresses",
DeliveryAddressKey: "DeliveryAddress",
AddressIDSelected: null,
RefreshCycle: 4000,
OrderStatus: ["Canceled", "Order Placed", "We Are Preparing Your Food", "In The Oven / Cooking", "Out For Delivery"],
Init: function() {
this.Menu = JoesPizza.Menu;
$("#PizzaOrderNext").click(this.OrderNext);
},
InitSubmit: function() {
this.LoadOrderDetails();
this.LoadOrderAddress();
},
LoadMenu: function() {
$("#MenuItemList").html("");
this.Menu.items.forEach(item => {
let html = HtmlCode.GetMenuItem(item);
$("#MenuItemList").append(html);
});
// attach click events to new menu items
let selector = "div[act=\"add2order\"]";
$(selector).click(function() {
//read values from the clicked menu item
let lbl = $(this).attr("lbl");
let cost = $(this).attr("cost");
App.OrderCostTotal += parseFloat(cost);
// html order item
let html = HtmlCode.GetOrderItem({
"lbl": lbl,
"cost": cost
});
$("#PizzaOrderItems").append(html);
App.OnAddUpdateOrderTotal();
// attach click events on order items
// add remove method; this can be improved
$(".order-item").off("click");
$(".order-item").click(function() {
let cost = $(this).attr("cost");
App.OrderCostTotal -= parseFloat(cost);
App.OnRemoveUpdateOrderTotal();
$(this).remove();
});
});
},
OnAddUpdateOrderTotal: function() {
// needs correction
let selector = '#PizzaOrderSummary, #PizzaOrderNext';
$(selector).css("visibility", "visible");
let total = "$" + App.OrderCostTotal.toFixed(2);
selector = "#PizzaOrderSummary > div:nth-child(2)";
$(selector).html(total);
},
OnRemoveUpdateOrderTotal: function(cost) {
// needs correction
if ($(".order-item").length == 1) {
let selector = "#PizzaOrderSummary, #PizzaOrderNext";
$(selector).css("visibility", "hidden");
return false;
} else {
let total = "$" + App.OrderCostTotal.toFixed(2);
let selector = "#PizzaOrderSummary > div:nth-child(2)";
$(selector).html(total);
return true;
}
},
OrderNext: function() {
if ($(".order-item").length == 0) {
alert("There seems to be a problem with your order.");
return false;
}
// read order info
let arr = [];
var total = 0;
$(".order-item").each((idx, item) => {
let lbl = $(item).attr("lbl");
let cost = $(item).attr("cost");
arr.push({
"lbl": lbl,
"cost": cost
});
});
// store order into
let json = JSON.stringify(arr);
localStorage.setItem(App.PendingOrderKey, json);
// move to submit page
// https://developer.mozilla.org/en-US/docs/Web/API/Window/localStorage
window.location.assign("/submit/");
},
LoadOrderDetails: function() {
let buff = localStorage.getItem(App.PendingOrderKey);
let order = JSON.parse(buff);
order.forEach(item => {
App.OrderCostTotal += parseFloat(item.cost);
let html = HtmlCode.GetOrderDetailsItem(item);
$("#OrderDetails").append(html);
});
let data = {
"lbl": "Total Cost:",
"cost": App.OrderCostTotal.toFixed(2)
};
let html = HtmlCode.GetOrderDetailsItem(data);
$("#OrderDetails").append(html);
},
LoadOrderAddress: function() {
let buff = localStorage.getItem(App.UsedAddressesKey);
if (buff == null) {
let html = "<div>Enter address to deliver your pizza</div>";
$("#AddressSelect").html(html);
} else {
let adrs = JSON.parse(buff);
let html = HtmlCode.GetAddressSelector("SelDelAdr", adrs);
$("#AddressSelect").html(html);
/* attach events */
$("#SelDelAdr").change(function() {
App.AddressIDSelected = parseInt($(this).val());
let adr = adrs[App.AddressIDSelected];
$("#txtStreetAddress").val(adr.street);
$("#txtCity").val(adr.city);
$("#txtZipCode").val(adr.zcode);
});
}
let selector = ".address-lines > div:last-child";
$(selector).click(App.PrePostOrder);
},
PrePostOrder: function() {
/* read address info */
let street = $("#txtStreetAddress").val();
let city = $("txtCity").val();
let zcode = $("txtZipCode").val();
/* homework... validate address info */
if (!App.validateAddressData(street, city, zcode)) {
alert("Please correct address info.");
return false;
}
/* create and store delivery address */
let adr = {
"street": street,
"city": city,
"zcode": zcode
};
localStorage.setItem(App.DeliveryAddressKey, JSON.stringify(adr));
/* cache address */
App.CacheCurrentAddress(adr);
/*
here we are ready to submit our pizza order to joe's pizzeria
we hide current frame and bring up status frame
http://api.jquery.com/fadeout/
*/
$(".submit-delivery").fadeOut(500, () => {
$(".submit-status").fadeIn(500, () => {
App.PostOrder();
})
});
},
CacheCurrentAddress: function(adr) {
if (App.AddressIDSelected != null)
return false;
let buff = localStorage.getItem(App.UsedAddressesKey);
let arr = (buff) ? JSON.parse(buff) : [];
if (App.IsAddressCached(arr, adr))
return false;
arr.push(adr);
buff = JSON.stringify(arr);
localStorage.setItem(App.UsedAddressesKey, buff);
},
IsAddressCached: function(arr, adr) {
let rval = false;
let street = adr.street.trim();
for (let idx in arr) {
let buff = arr[idx].street.trim();
if (buff === street) {
rval = true;
break;
}
}
return rval;
},
validateAddressData: function(street, city, zcode) {
let rval = true;
/*
check if passed values are null, undefined or empty strings
if so reject... return false;
rval = false
*/
/* if (street !== "" && city !== "" && zcode !== null){
return rval;
} else {
rval = false;
*/
return rval;
/* }
*/
},
PostOrder: function() {
/*alert("posting order"); */
let order = localStorage.getItem(App.PendingOrderKey);
let addr = localStorage.getItem(App.DeliveryAddressKey);
/* place order */
let backend = new ClientBackend();
backend.PostOrder(order, addr, (msg) => {
let orderid = parseInt(msg);
if (orderid) {
$("#feedbackMsg").html(`Your Order ID: ${orderid}`);
/* start to monitor order status */
setInterval(App.MonitorOrderStatus, App.RefreshCycle, orderid);
} else {
$("#feedbackMsg").html(msg);
}
});
},
MonitorOrderStatus: function(orderid) {
let backend = new ClientBackend();
backend.MonitorOrderStatus(orderid, (scode) => {
let status = App.OrderStatus[parseInt(scode)];
let dts = new Date().toLocaleTimeString();
let msg = `Your Order ID: ${orderid}; <i>${status};</i> ${dts}`;
$("#feedbackMsg").html(msg);
});
}
}
function newFunction() {
return "SelDelAdr";
}
**HTML-TEMPLATE.JS**
var HtmlCode = {
GetMenuItem: function(item) {
let plg = item.choices[0];
let prg = item.choices[1];
let lbl_plg = `${item.type} - ${item.name} - ${plg.size}`;
let lbl_prg = `${item.type} - ${item.name} - ${prg.size}`;
return `<div class= "menu-item">
<div><div><img src="${item.img}"></div><div>${item.name}</div>
</div><div>${item.descr}</div>
<div>
<div act="add2order" id="${plg.id}" cost="${plg.cost}" lbl="${lbl_plg}" title="Click to order">${plg.txt}</div>
<div act="add2order" id="${prg.id}" cost="${prg.cost}" lbl="${lbl_prg}" title="Click to order">${prg.txt}</div>
</div>
</div>`;
},
GetOrderItem: function(item) {
return `<div class="order-item" cost="${item.cost}" lbl="${item.lbl}"
title="Click to remove">
<div>${item.lbl}</div><div>$${item.cost}</div></div>`;
},
GetOderItem: function(item) {
return `<div class="order-item" cost="${item.cost}" lbl="${item.lbl}"
title="Click to remove">
<div>${item.lbl}</div><div>$${item.cost}</div></div>`;
},
GetOrderDetailsItem: function(item) {
return `<div class="sd-item-details"><div>${item.lbl}</div>
<div>$${item.cost}</div></div>`;
},
GetAddressSelector: function(id, adrs) {
let buff = "<option value=\"\" selected=\"selected\">--- Select Address ---</option>";
adrs.forEach((adr, idx) => {
buff += `<option value="${idx}">${adr.street}</option>`;
});
return `<select id="${id}">${buff}</select>`;
},
}
**MENU.JS**
var JoesPizza = JoesPizza || {};
JoesPizza.Menu = {
"items": [
{
"type": "Pizza",
"name": "Cheese",
"descr": "Marinara sauce topped with whole milk mozzarella cheese.",
"choices": [{
"id": "pizza-cheese-lg",
"size": "Large",
"cost": 22.99,
"txt": "Large: $22.99"
},
{
"id": "pizza-cheese-rg",
"size": "Regular",
"cost": 18.99,
"txt": "Regular: $18.99"
}
],
"img": "/imgs/cheese.png"
},
{
"type": "Pizza",
"name": "Pepperoni",
"descr": "Marinara sauce with authentic old-world style pepperoni.",
"choices": [{
"id": "pepp-lg",
"size": "Large",
"cost": 23.99,
"txt": "Large: $23.99"
},
{
"id": "pepp-rg",
"size": "Regular",
"cost": 19.99,
"txt": "Regular: $19.99"
}
],
"img": "/imgs/pepperoni.png"
},
{
"type": "Pizza",
"name": "Meat Lover's",
"descr": "Marinara sauce, authentic pepperoni, natural Italian sausage, roasted ham, smoked bacon, pork and beef.",
"choices": [{
"id": "meat-lg",
"size": "Large",
"cost": 23.99,
"txt": "Large: $23.99"
},
{
"id": "meat-rg",
"size": "Regular",
"cost": 19.99,
"txt": "Regular: $19.99"
}
],
"img": "/imgs/meat.png"
},
{
"type": "Pizza",
"name": "Supreme",
"descr": "Marinara sauce, pepperoni, pork, beef,fresh mushrooms, fresh green bell peppers and fresh red onions.",
"choices": [{
"id": "supr-lg",
"size": "Large",
"cost": 23.99,
"txt": "Large: $23.99"
},
{
"id": "supr-rg",
"size": "Regular",
"cost": 19.99,
"txt": "Regular: $19.99"
}
],
"img": "/imgs/supreme.png"
},
{
"type": "Wings",
"name": "Traditional Bone-in",
"descr": "Classic, juicy bone-in wings served perfectly crispy and tossed in your choice of signature sauce.",
"choices": [{
"id": "wings-trad-12",
"size": "12 Pieces",
"cost": 11.99,
"txt": "12 Wings: $11.99"
},
{
"id": "wings-trad-08",
"size": "8 Pieces",
"cost": 8.99,
"txt": "8 Wings: $8.99"
}
],
"img": "/imgs/wings.png"
}
]
};
**APP.CSS**
.page-top {
width: 98%;
max-width: 1200px;
margin: auto;
margin-top: 2px;
height: 64px;
line-height: 64px;
font-size: 48px;
font-weight: bold;
letter-spacing: 4px;
border: 2px solid darkgrey;
border-radius: 32px;
/* https://developer.mozilla.org/en-US/docs/Web/CSS/linear-gradient */
background: linear-gradient(30deg, green, white, red);
}
.page-top>img:nth-child(1) {
float: left;
margin-right: 24px;
}
.page-top>a {
float: right;
margin-right: 24px;
margin-top: 8px;
}
.page-body {
width: 96%;
max-width: 1160px;
margin: auto;
margin-top: 8px;
height: auto;
border: 0px dotted blue;
}
/*
order.html
*/
.side-left,
.side-right {
display: inline-block;
width: 48%;
}
/* left side of the page */
#PizzaMenu {
height: 480px;
border-radius: 4px;
overflow-y: scroll;
vertical-align: top;
}
#PizzaOrder {
height: 480px;
border-radius: 4px;
vertical-align: top;
overflow-y: scroll;
}
#PizzaMenu>legend,
#PizzaOrder>legend {
font-size: 24px;
}
/*
tell students to group classes next to each other for a given topic
*/
.menu-item {
border: 2px solid #dcdcdc;
border-radius: 16px;
margin-top: 4px;
}
.menu-item>div:nth-child(1) {
height: 64px;
line-height: 64px;
border: 1px dotted transparent;
background-repeat: no-repeat;
background-size: contain;
background-position: 16px;
font-size: 32px;
}
.menu-item>div:nth-child(1)>div {
display: inline-block;
height: inherit;
vertical-align: top;
}
.menu-item>div:nth-child(1)>div>img {
height: inherit;
margin-left: 16px;
}
.menu-item>div:nth-child(2) {
width: 96%;
margin: auto;
font-size: 16px;
border: 1px dotted transparent;
}
.menu-item>div:nth-child(3) {
text-align: right;
}
.menu-item>div:nth-child(3)>div {
display: inline-block;
width: 120px;
border: 1px solid #dcdcdc;
border-radius: 4px;
text-align: center;
padding: 4px;
margin: 4px 16px 4px 0px;
}
.menu-item>div:nth-child(3)>div:hover {
cursor: pointer;
background-color: lightgreen;
}
/* right side of the page */
#PizzaOrderSummary {
width: 80%;
height: 32px;
line-height: 32px;
font-size: 24px;
margin: auto;
margin-top: 16px;
border: 1px solid #aab7b8;
border-radius: 16px;
visibility: hidden;
}
#PizzaOrderSummary>div:nth-child(1) {
display: inline-block;
width: 48%;
text-indent: 12px;
}
#PizzaOrderSummary>div:nth-child(2) {
display: inline-block;
width: 48%;
vertical-align: top;
text-align: right;
}
/* right side */
#PizzaOrderNext {
width: 60%;
height: 40px;
line-height: 40px;
font-size: 20px;
text-align: center;
margin: auto;
margin-top: 8px;
border: 1px solid #aab7b8;
border-radius: 16px;
visibility: hidden;
}
#PizzaOrderNext:hover {
cursor: pointer;
background-color: lightgreen;
}
.order-item {
width: 98%;
margin: auto;
margin-top: 4px;
height: 24px;
line-height: 24px;
font-size: 18px;
border: 1px solid #aab7b8;
border-radius: 12px;
}
.order-item:hover {
cursor: pointer;
background-color: #f9b2b2;
}
.order-item>div:nth-child(1) {
display: inline-block;
width: 80%;
text-indent: 12px;
}
.order-item>div:nth-child(2) {
display: inline-block;
width: 16%;
margin-right: 8px;
text-align: right;
}
/*
submit.html
*/
.submit-delivery {
border: 2px solid #aab7b8;
border-radius: 8px;
height: 520px;
}
.submit-status {
display: none;
border: 2px solid #aab7b8;
border-radius: 8px;
height: 520px;
}
.sd-order-details,
.sd-address {
display: inline-block;
width: 49%;
border: 0px solid #aab7b8;
margin-left: 5px;
height: 520px;
vertical-align: top;
}
/*
.sd-address {
display: inline-block;
width: 49%;
height: 520px;
border: 0px solid #aab7b8;
margin-left: 5px;
}
*/
.sd-order-details>fieldset,
.sd-address>fieldset {
height: 500px;
border-radius: 8px;
}
.sd-order-details legend,
.sd-address legend {
font-size: 24px;
margin-left: 14px;
}
.sd-item-details {
width: 98%;
height: 24px;
line-height: 24px;
font-size: 18px;
border: 1px solid #aab7b8;
border-radius: 12px;
margin-top: 4px;
}
.sd-item-details>div:nth-child(1) {
display: inline-block;
width: 75%;
text-indent: 8px;
}
.sd-item-details>div:nth-child(2) {
display: inline-block;
width: 22%;
text-align: right;
}
.sd-item-details:last-child {
width: 80%;
margin: auto;
margin-top: 16px;
}
/* Address Lines */
.address-lines {
font-size: 16px;
}
.address-lines>div {
width: 96%;
margin: auto;
margin-top: 12px;
height: 64px;
border: 1px solid #aab7b8;
border-radius: 32px;
}
.address-lines>div:last-child {
width: 96%;
margin: auto;
margin-top: 24px;
height: 48px;
line-height: 48px;
text-align: center;
border: 1px solid #aab7b8;
border-radius: 24px;
font-size: 24px;
}
.address-lines>div:last-child:hover {
cursor: pointer;
background-color: lightgreen;
}
.address-lines>div>div {
margin-top: 6px;
}
.address-lines input[type=text] {
width: 88%;
margin-left: 5.5%;
}
.address-lines label {
margin-left: 6%;
}
#AddressSelect {
line-height: 64px;
font-size: 24px;
vertical-align: top;
margin-top: 0px !important;
}
#AddressSelect>div {
font-size: 24px;
width: 88.5%;
margin-left: 5.5%;
text-align: center;
}
#AddressSelect>select {
font-size: 22px;
width: 88.5%;
margin-left: 5.5%;
}
#feedbackMsg {
width: 80%;
margin: auto;
height: 96px;
line-height: 96px;
text-align: center;
font-size: 28px;
border: 2px solid #aab7b8;
border-radius: 32px;
margin-top: 128px;
}
#feedbackMsg i {
color: darkgreen;
}
**SUBMIT.HTML**
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF8" />
<title>Joe's Pizza</title>
<link type="text/css" rel="stylesheet" href="/css/app.css" />
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script type="text/javascript" src="/js/html-templates.js"></script>
<script type="text/javascript" src="/js/clt-backend.js"></script>
<script type="text/javascript" src="/js/app.js"></script>
</head>
<body>
<!-- top banner -->
<div class="page-top">
<img src="/imgs/pizza.png" title="Pizza Picture" />
<span>joe's pizza</span>
<a href="/">
<img src="/imgs/home.png"></a>
<a href="/orders/">
<img src="/imgs/order.png"></a>
</div>
<div class="page-body">
<!-- submit delivery html frame -->
<div class="submit-delivery">
<div class="sd-order-details">
<fieldset style="overflow-y:scroll;">
<legend>order details</legend>
<!-- correct id in js code -->
<div id="OrderDetails">
</div>
</fieldset>
</div>
<div class="sd-address">
<fieldset>
<legend>delivery address</legend>
<div id="DeliveryAddress">
<div class="address-lines">
<div>
<div id="AddressSelect">
</div>
</div>
<div>
<div>
<label for="txtStreetAddress">enter street address</label>
<input type="text" id="txtStreetAddress" placeholder="exp: 8012 Austin Street" />
</div>
</div>
<div>
<div>
<label for="txtCity">enter city, state</label>
<input type="text" id="txtCity" placeholder="exp: Queens, NY" />
</div>
</div>
<div>
<div>
<label for="txtZipCode">enter zip code</label>
<input type="text" id="txtZipCode" placeholder="exp: 11415" />
</div>
</div>
<!-- get pizza button -->
<div id="btnDeliver">Click To Get Pizza</div>
</div>
</div>
</fieldset>
</div>
</div>
<div class="submit-status">
<div id="feedbackMsg"></div>
</div>
</div>
<script type="text/javascript">
App.InitSubmit();
</script>
</body>
</html>
最佳答案
您将值以字符串格式存储在本地存储中作为
// store order into
let json = JSON.stringify(arr);
localStorage.setItem(App.PendingOrderKey, json);
因此在解析它时,您必须将它转换回 JSON 对象,以便您可以使用它的属性。
你得到的是
let buff = localStorage.getItem(App.PendingOrderKey);
应该是什么
let buff = JSON.parse(localStorage.getItem(App.PendingOrderKey));
它将字符串化对象转换为 JSON 对象。
关于javascript - 本地存储只存储我的部分数据, session 存储中没有存储任何数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53160165/
我有一个 html 格式的表单: 我需要得到 JavaScript在value input 字段执行,但只能通过表单的 submit .原因是页面是一个模板所以我不控制它(不能有
我管理的论坛是托管软件,因此我无法访问源代码,我只能向页面添加 JavaScript 来实现我需要完成的任务。 我正在尝试用超链接替换所有页面上某些文本关键字的第一个实例。我还根据国家/地区代码对这些
我正在使用 JS 打开新页面并将 HTML 代码写入其中,但是当我尝试使用 document.write() 在新页面中编写 JS 时功能不起作用。显然,一旦看到 ,主 JS 就会关闭。用于即将打开的
提问不是为了解决问题,提问是为了更好地理解系统 专家!我知道每当你将 javascript 代码输入 javascript 引擎时,它会立即由 javascript 引擎执行。由于没有看过Engi
我在一个文件夹中有两个 javascript 文件。我想将一个变量的 javascript 文件传递到另一个。我应该使用什么程序? 最佳答案 window.postMessage用于跨文档消息。使
我有一个练习,我需要输入两个输入并检查它们是否都等于一个。 如果是 console.log 正则 console.log false 我试过这样的事情: function isPositive(fir
我正在做一个Web应用程序,计划允许其他网站(客户端)在其页面上嵌入以下javascript: 我的网络应用程序位于 http://example.org 。 我不能假设客户端网站的页面有 JQue
目前我正在使用三个外部 JS 文件。 我喜欢将所有三个 JS 文件合而为一。 尽一切可能。我创建 aio.js 并在 aio.js 中 src="https://code.jquery.com/
我有例如像这样的数组: var myArray = []; var item1 = { start: '08:00', end: '09:30' } var item2 = {
所以我正在制作一个 Chrome 扩展,它使用我制作的一些 TamperMonkey 脚本。我想要一个“主”javascript 文件,您可以在其中包含并执行其他脚本。我很擅长使用以下行将其他 jav
我有 A、B html 和 A、B javascript 文件。 并且,如何将 A JavaScript 中使用的全局变量直接移动到 B JavaScript 中? 示例 JavaScript) va
我需要将以下整个代码放入名为 activate.js 的 JavaScript 中。你能告诉我怎么做吗? var int = new int({ seconds: 30, mark
我已经为我的 .net Web 应用程序创建了母版页 EXAMPLE1.Master。他们的 I 将值存储在 JavaScript 变量中。我想在另一个 JS 文件中检索该变量。 示例1.大师:-
是否有任何库可以用来转换这样的代码: function () { var a = 1; } 像这样的代码: function () { var a = 1; } 在我的浏览器中。因为我在 Gi
我收到语法缺失 ) 错误 $(document).ready(function changeText() { var p = document.getElementById('bidp
我正在制作进度条。它有一个标签。我想调整某个脚本完成的标签。在找到可能的解决方案的一些答案后,我想出了以下脚本。第一个启动并按预期工作。然而,第二个却没有。它出什么问题了?代码如下: HTML:
这里有一个很简单的问题,我简单的头脑无法回答:为什么我在外部库中加载时,下面的匿名和onload函数没有运行?我错过了一些非常非常基本的东西。 Library.js 只有一行:console.log(
我知道 javascript 是一种客户端语言,但如果实际代码中嵌入的 javascript 代码以某种方式与在控制台上运行的代码不同,我会尝试找到答案。让我用一个例子来解释它: 我想创建一个像 Mi
我如何将这个内联 javascript 更改为 Unobtrusive JavaScript? 谢谢! 感谢您的回答,但它不起作用。我的代码是: PHP js文件 document.getElem
我正在寻找将简单的 JavaScript 对象“转储”到动态生成的 JavaScript 源代码中的最优雅的方法。 目的:假设我们有 node.js 服务器生成 HTML。我们在服务器端有一个对象x。
我是一名优秀的程序员,十分优秀!