JavaScript 系列一:第6課

認識資料型態與轉換 課程目標

by applelily

HTML

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>線上下單</title>
    <link rel="stylesheet" href="./js_parseInt.css">
</head>
<body>
    <div class="bk_img_container">
        <div class="bk_img">
            <img src="https://imgur.com/Y4gjfmp.jpg">
        </div>

    </div>
    <div class="orderPage">
        <div class="order_input_container">

            <div class="list_items">
                <span class="inline">
                     您好,尊姓大名?
                    <input class="input input_cust list_itms" id="customer_name" type="text"  autofocus /> 
                </span>
                <span class="inline">
              
                    服裝的分類: 
                    <select id="list_item_1">
                        <option value="men">男裝</option>
                        <option value="women">女裝</option>
                    </select>
                 </span>
                 <span class="inline">
                    服裝的類型:
                    <select id="list_itme_2">
                        <option value="coats">外套</option>
                        <option value="top">上衣</option>
                        <option value="bottom">下身</option>
                    </select>
                </span>
                <span class="inline">
                    訂購數量:
                   <input class="input list_itms" id="order_qty" type="text"/> 
                   <span style="color: white;
                    background-color:  #653705  ;
                    padding: 5px 10px;">
                   ( 男裝:600元/件 ,女裝:500元/件 )</span>
               </span>
            </div>
                
            <div class="btn_container">
                <button class="btn btn_1" onclick="goOrder()">我要訂購</button>
                <button class="btn btn_2" onclick="aboutMe()">認識工廠</button>
            </div>

        </div>

    
        <div class="order_container">
           ...

CSS

body{
    color: #333;
    background-color: #eedada;
    font-family: OpenSansRegular, "微軟正黑體修正", "微軟正黑體", "Microsoft JhengHei", MHei, STHeitiTC-Light, sans-serif, Arial, Helvetica, "Helvetica Neue", Tahoma, Verdana;

}



.bk_img_container {
    position: relative;
    overflow:scroll;
    width: 100%;
    height: 98vh; /* 占满整个视口高度 */
}

.bk_img {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    object-fit: cover; /* 铺满且不变形 */
    z-index: -1;
}

.orderPage{
    position: absolute;
    top: 20px;
    left:20px; 
    background-color:  rgba(238, 218, 218 , 50%) ;
    padding-left: 20px; 
    font-size:  1.2rem;
    color:  #653705;
}
.order_input_container{
    position: relative;
    width: 799px;
    height: 290px;

}

.list_items {
    margin-top: 8px;
    display: flex;
    flex-direction: column;
    align-items: flex-start;
}



.inline {
    display: inline;
    padding-top: 12px;
}

.input{
    margin-top: 6px;
    margin-right: 20px;
    width: 150px;
    height: 30px;
    border: 1px solid #653705 ;
    background-color: white;
   
}
.input_cust:focus { 
    outline: none !important;
    border-color: #653705;
    box-shadow: 0 0 10px #653705;
 }

 select {
    font-size: 17px;
    background: rgb(101, 55, 5, 70%);
    color: #fafafa;
    width: 100px;
    height: 2em;
    padding: 6px;
    position: relative;
    border-radius: 2px;
 }
select:focus {
        border: none; 
        font-size: 20px;
      }
select::after {
        font-size: 15px;
      }

 .btn_container{
    position: relative;
    top: 20px;
    left: 400px;
   
 }
.btn {
    margin-right: 20px;
    font-size: 1.2rem;
    width: 150px;
    height: 50px;
    background-color: #ffeae4;
    color: #653705;
    border:0px;
    cursor: pointer;
}


.btn_2{
    color: white;
    background-color: #653705;
}

.btn:hover{
    transform: scale(1.1);
}


.br_2 {
    display: block;
    content: "";
    border-bottom: 1px solid #653705;
    
 ...

JavaScript

function goOrder(){
    const custName= document.getElementById('customer_name').value;
    let order_qty = document.getElementById('order_qty').value;
    let msg = '';
    order_qty = parseInt(order_qty);
    if ((custName == '') || (custName == null)) {
        msg = "請輸入姓名 !!" ;
        alert(msg);
        document.getElementById('customer_name').focus();
    }
    else if ((isNaN(order_qty)))  {
       
        msg = "訂購數量 -》數字 !!" ;
        alert(msg);
        document.getElementById('order_qty').focus();
    } else {

        const myMsg = '謝謝您對我們的衣服有興趣!請致電 0987-654-321,會有專人提供您報價!';
        const msg = custName + ', '+ myMsg;
        alert(msg);

        /* show 出訂單訊息 */
        document.querySelector('.order_container').style.display = 'block';
        
        document.getElementById('order_list_customer_name').textContent=custName;

        let menu_1 = document.getElementById('list_item_1');
        let menu_1_index = menu_1.selectedIndex;
        let menu_1_value = menu_1.options[menu_1_index].value;
        let menu_1_text = menu_1.options[menu_1_index].text;
        document.getElementById('order_list_item_1').textContent=menu_1_text;
        let price = 0;
        let promotion_msg = '「最近剛好是工廠週年慶,只要您今天來電下單,'
        if (menu_1_value == 'men') {
            price = 600;
            alert( promotion_msg+'我們將贈送您帥氣領帶!」');
        } else if  (menu_1_value == 'women'){
            price = 500;
            alert( promotion_msg+'我們將贈送您美麗圍巾!」');
        }

        let menu_2 = document.getElementById('list_itme_2');
        let menu_2_index = menu_2.selectedIndex;
        let menu_2_value = menu_2.options[menu_1_index].value;
        let menu_2_text = menu_2.options[menu_2_index].text;
        document.getElementById('order_list_item_2').textContent=menu_2_text;
        
        document.getElementById('list_order_qty').textContent=order_qty;
        let amt = 0;
        amt =  price * order_qty;
        let amt_formatted= amt.toLocaleString('en-US');
     ...