JSFiddle - React, Tailwind, and code Playground
by kushmuffin
HTML
<fieldset>
<legend>
輸入訂單
</legend>
<section>
<input id="username" type="text" value="" placeholder=" 您的姓名" />
<select id="genderType">
<option value="" selected disabled>分類</option>
<option value="male">男裝</option>
<option value="female">女裝</option>
</select>
<select id="clothesType">
<option value="" selected disabled>類型</option>
<option value="coat">外套</option>
<option value="jacket">上衣</option>
<option value="Pants">下身</option>
</select>
<input id="clothesCount" value="" type="number" min="0" placeholder=" 訂購數量">
<div>
<button class="btn" onclick="order()">我想訂購</button>
<button class="btn" onclick="aboutUs()">認識工廠</button>
</div>
<div class="list">
<span>您的訂單</span>
<br>
<span>顧客姓名:</span><span id="user"></span>
<br>
<span>服裝分類:</span><span id="gender"></span>
<br>
<span>服裝類型:</span><span id="clothes"></span>
<br>
<span>訂購數量:</span><span id="count"></span>
<br>
<span>初步估價:</span><span id="price"></span>
</div>
</section>
</fieldset>
CSS
* {
box-sizing: border-box;
}
body {
display: flex;
justify-content: center;
}
fieldset {
padding: 1em;
width: 50%;
border: 1px solid rgba(0, 0, 255, 0.5);
border-radius: 0.5em;
}
section {
display: flex;
flex-direction: column;
align-items: center;
}
select {
display: block;
margin: 0.3rem;
padding: 3px;
border: 1px solid gray;
border-radius: 5px;
width: 70%;
}
input {
display: block;
margin: 0.3rem;
padding: 3px;
border: 1px solid gray;
border-radius: 5px;
width: 70%;
}
.btn {
margin: 5px;
padding: 5px;
border: 0.5px;
border-radius: 10px;
}
.btn:hover {
color: white;
background-color: gray;
transform: translateX(5px);
transition: transform 0.4s linear 0s;
}
.list {
width: 60%;
padding: 0.5rem;
background-color: lightgray;
border-radius: 5px;
}
JavaScript
function order() {
//名稱
let name = document.getElementById('username').value;
//服裝分類
let gender = document.getElementById('genderType');
let genderIndex = gender.selectedIndex;
let genderValue = gender.options[genderIndex].value;
let genderText = gender.options[genderIndex].text;
//服裝類型
let typesIndex =
document.querySelector("#clothesType").selectedIndex;
let typesText =
document.querySelector("#clothesType").options[typesIndex].text;
let typesValue =
document.querySelector("#clothesType").options[typesIndex].value;
//訂購數量
let countIndex =
document.querySelector("#clothesCount").value;
let clothesCount = document.getElementById('clothesCount').value;
//判斷男女裝
/* if(genderText == gender.option[1].value){
let menPrice = 600;
} else if(genderText == gender.option[2].value){
let womenPrice = 500;
} */
//計算
let price = calculation();
valuation = price * parseInt(countIndex)
if (name == "") {
alert("煩請填寫您的大名")
} else if (gender == "" || typesIndex == "") {
alert("請選擇分類及類型")
} else if(countIndex < 100){
alert("抱歉,我們工廠的最低出貨量是 100 件")
} else if(countIndex > 5000){
alert("抱歉,我們工廠的最高出貨量是 5000 件")
} else {
alert(name + '您好!\n謝謝您對我們的衣服有興趣!請致電 0987-654-321,會有專人提供您報價!');
if (genderValue == "male") {
alert('最近剛好是工廠週年慶,只要您今天來電下單,我們將贈送您帥氣領帶!')
} else if (genderValue == "female") {
alert('最近剛好是工廠週年慶,只要您今天來電下單,我們將贈送您美麗圍巾!')
}
alert('您的姓名:' + name + '\n服裝分類:' + genderText + '\n服裝類型:' + typesText + '\n訂購數量:' + countIndex + '\n初步估價:' + valuation);
/* alert */
document.getElementById('user').textContent = name;
document.getElementById('gender').textContent = genderText;
/* document.querySelector("#clothes").textContent = typesText; */
document.getElementById('clothes').textContent = typesText;
document.getElementById('count').textContent = countIndex;
document.getElementById('price').textContent = valuation;
}
}
//認識工廠
function...