gpt4 book ai didi

javascript - 如何从两个表单值中获取总计?

转载 作者:行者123 更新时间:2023-11-28 15:27:23 24 4
gpt4 key购买 nike

我最近决定自学 JavaScript/HTML/CSS 以创建网站。一个月前我开始自学,并且一直在一个网站上工作,看看到目前为止我能做什么,我想包含一些基本的 JavaScript 以使网站具有交互性。到目前为止,在开始我的元素时,我遇到了 2 个问题......

  1. 到目前为止,我有三个函数可以在您单击提交按钮时显示您的姓名、您采摘的花的类型以及您希望购买的花的数量。我被卡住的地方是我希望在“评论”div 中有一个“订单总计”部分,我希望在它下面显示它将花费的总价,这将是花的类型 * 花的数量。所以我基本上不知道如何制作一个函数,该函数将从花的类型和花的数量中获取表单值,并在单击提交按钮时让它给出订单总数。对于这个测试站点,我有3 朵花:向日葵、玫瑰和百合……假设向日葵 5 美元,玫瑰 6 美元,百合 7 美元。

  2. 当我在浏览器(我使用 chrome)中查看我的网站并点击刷新时,它会改变我在导航部分中的按钮大小,比它们最初显示的小得多。此外,当我尝试向我的导航按钮添加“:悬停”选项以让它们在我将鼠标悬停在它们上方时改变颜色时,什么也不会发生。我感觉这两个问题都与我的按钮有关,但我不知道为什么会出现这些问题。

这是我在这里发表的第一篇文章,所以我希望我做对了,并提供了有关我的问题的足够信息。非常感谢为这个新手提供有关这些问题的任何帮助,谢谢。

代码:

body {
background: #ff0055;
background: linear-gradient(#b30086, #ff0055) fixed;

}

.header {
width: 740px;
height: 130px;
margin: 20px auto 10px auto;
background: #ff80df;
background: linear-gradient(#ff80df, #ccffdd);
border-radius: 10px 50px 10px 10px;
box-shadow: 5px 5px 10px #3c2f00;
line-height: 130px;
}

.slogan {
font-size: 30px;
float: right;
font-family: 'Syncopate', sans-serif;
margin-right: 50px;
}

.workarea {


width:714px;
height: 417px;
background: #ff80df;
background: linear-gradient(#ccffdd, #ff80df);
border-radius: 10px 10px 10px 50px;
box-shadow: 5px 5px 10px #3c2f00;
padding: 20px;
clear: both;
float: left;
margin: 20px auto 20px auto;
position: absolute;
margin-left: 250px;
}

.review {
background: green;
width: 180px;
height: 417px;
margin: 20px auto 20px auto;
float: left;
margin-left:1025px;
position: relative;
padding: 20px;
border-radius: 10px 50px 50px 10px;
box-shadow: 5px 5px 10px #3c2f00;
background: #ff80df;
background: linear-gradient(#ccffdd, #ff80df);

}

button {
display: block;
width: 150%;
padding: 20%;
border-radius: 50px 10px 10px 50px;
margin-bottom: 30px;
margin: 20px;
background: #ff80df;
background: linear-gradient(#ccffdd, #ff80df);
box-shadow: 5px 5px 10px #3c2f00;
font-weight: bold;



}

.nav {
position: absolute;
margin: 40px 40px 40px -10px;
margin-top: 40px;

}

.nav button:not(:last-child) {
border-bottom: none;
}

.nav button:hover {
background-color: green;
}

#but2, #but3 {
margin-top: 50px;
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title></title>
<link rel="stylesheet" href="flower.css" type="text/css">
<link href="https://fonts.googleapis.com/css?family=Syncopate" rel="stylesheet">
<script>
function showName() {
document.getElementById("display").innerHTML = document.getElementById("name").value;
}

function showType() {
document.getElementById("display2").innerHTML = document.getElementById("typeFlo").value;
}

function showNumber() {
document.getElementById("display3").innerHTML = document.getElementById("numFlo").value;



}

</script>

</head>

<body>
<div class="header">
<span class="slogan">Miss Lemon's Flower Shop</span>


</div>


<div class="workarea">

<h3>Place your order below!</h3>

<form>
<p>Your Name:
<input type="text" id="name" placeholder="Enter your name"></p><br>
<p>Type of flower:
<select id="typeFlo">
<option>Sunflower</option>
<option>Roses</option>
<option>Lily</option>
</select></p><br>
<p>Number of flowers:
<input type="number" id="numFlo" placeholder="How many flowers?"></p><br>
</form>
<input type="submit" onclick="showName(); showType(); showNumber();">
</div>

<div class="review">
<b><span id="display" style="color: red"></span><br> Review your order below</b><br><br><br>

<b>Type of flower:</b><br>
<span id="display2"></span>
<br><br>
<b>Number of Flowers:</b><br>
<span id="display3"></span>
<br><br>
<b>Order Total:</b><br>
<span id="display4"></span>
<br><br><br>
<input type="submit" value="checkout">

</div>

<div class="nav">
<button id="but1">Home</button>
<button id="but2">Products</button>
<button id="but3">Buy Now</button>

</div>



</body>
</html>

最佳答案

首先,我用实际标签替换了您输入的 p 标签。然后我将第一个按钮更改为实际按钮而不是提交按钮。然后我创建了另一个函数 showTotal() ,它将获取花的类型并从数组中获取成本并将总计放入订单总计 div 中。虽然没有理由不能将所有这四个功能结合起来。

showTotal 函数将花的类型值存储在一个对象中,读取选定的花的类型和花的数量,计算总数并将其显示为 2 位小数。

悬停不起作用的原因是背景:线性渐变。你的背景颜色在变化,它就在线性渐变之下。

How to check if a variable is an integer in JavaScript? https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/toFixed

function showName() 
{
document.getElementById("display").innerHTML = document.getElementById("name").value;
}

function showType()
{
document.getElementById("display2").innerHTML = document.getElementById("typeFlo").value;
}

function showNumber()
{
document.getElementById("display3").innerHTML = document.getElementById("numFlo").value;
}

function showTotal()
{
//flower prices in object
var flowerPrices = {
'Sunflower': 5,
'Roses': 6,
'Lily': 7
};
var numberFlowers = document.getElementById('numFlo').value;
var flowerType = document.getElementById('typeFlo').value;
var total = 0;
var totalContainer = document.getElementById('display4');

//check for integer
if (!isNaN(numberFlowers) &&
parseInt(Number(numberFlowers)) == numberFlowers &&
!isNaN(parseInt(numberFlowers, 10)) &&
//check that valid flower is selected
flowerPrices.hasOwnProperty(flowerType)
) {
//get total
total = flowerPrices[flowerType] * numberFlowers;
//format to 2 decimal places
totalContainer.innerHTML = '$' + total.toFixed(2);
}
}
body {
background: #ff0055;
background: linear-gradient(#b30086, #ff0055) fixed;

}

.header {
width: 740px;
height: 130px;
margin: 20px auto 10px auto;
background: #ff80df;
background: linear-gradient(#ff80df, #ccffdd);
border-radius: 10px 50px 10px 10px;
box-shadow: 5px 5px 10px #3c2f00;
line-height: 130px;
}

.slogan {
font-size: 30px;
float: right;
font-family: 'Syncopate', sans-serif;
margin-right: 50px;
}

.order-label {
display: inline-block;
width: 128px;
}

.workarea {
width:714px;
height: 417px;
background: #ff80df;
background: linear-gradient(#ccffdd, #ff80df);
border-radius: 10px 10px 10px 50px;
box-shadow: 5px 5px 10px #3c2f00;
padding: 20px;
clear: both;
float: left;
margin: 20px auto 20px auto;
position: absolute;
margin-left: 250px;
}

.review {
background: green;
width: 180px;
height: 417px;
margin: 20px auto 20px auto;
float: left;
margin-left:1025px;
position: relative;
padding: 20px;
border-radius: 10px 50px 50px 10px;
box-shadow: 5px 5px 10px #3c2f00;
background: #ff80df;
background: linear-gradient(#ccffdd, #ff80df);

}

button {
display: block;
width: 150%;
padding: 20%;
border-radius: 50px 10px 10px 50px;
margin-bottom: 30px;
margin: 20px;
background: #ff80df;
background: linear-gradient(#ccffdd, #ff80df);
box-shadow: 5px 5px 10px #3c2f00;
font-weight: bold;
}

.nav {
position: absolute;
margin: 40px 40px 40px -10px;
margin-top: 40px;

}

.nav button:not(:last-child) {
border-bottom: none;
}

.nav button:hover {
background: linear-gradient(#0F0, #ff80df);
}

#but2, #but3 {
margin-top: 50px;
}

#but1:hover {
color: #00F;
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title></title>
<link rel="stylesheet" href="flower.css" type="text/css">
<link href="https://fonts.googleapis.com/css?family=Syncopate" rel="stylesheet">
</head>

<body>
<div class="header">
<span class="slogan">Miss Lemon's Flower Shop</span>
</div>

<div class="workarea">
<h3>Place your order below!</h3>
<form>
<label for="name" class="order-label">Your Name:</label>
<input type="text" id="name" placeholder="Enter your name"><br>

<label for="typeFlo" class="order-label">Type of flower: </label>
<select id="typeFlo">
<option>Sunflower</option>
<option>Roses</option>
<option>Lily</option>
</select><br>

<label for="numFlo" class="order-label">Number of flowers: </label>
<input type="number" id="numFlo" placeholder="How many flowers?"><br>
</form>
<input type="button" onclick="showName(); showType(); showNumber(); showTotal();" value="Show Totals">
</div>

<div class="review">
<b><span id="display" style="color: red"></span><br> Review your order below</b><br><br><br>

<b>Type of flower:</b><br>
<span id="display2"></span>
<br><br>
<b>Number of Flowers:</b><br>
<span id="display3"></span>
<br><br>
<b>Order Total:</b><br>
<span id="display4"></span>
<br><br><br>
<input type="submit" value="checkout">
</div>

<div class="nav">
<button id="but1">Home</button>
<button id="but2">Products</button>
<button id="but3">Buy Now</button>
</div>
</body>
</html>

关于javascript - 如何从两个表单值中获取总计?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45093560/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com