JavaScript 系列一:第5課

基本的 if/else 條件流程控制

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_ifelse.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_list_container">

            <div class="list_items">
                <span class="inline">
                     您好,尊姓大名?
                    <input class="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>
            </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">
            <div class="order_list">
                <p>您的訂單</p>
                <br class="br_2">
                <p >顧客姓名: <span id="order_list_customer_name"></span>
                </p>
                <p >服裝分類: <span id="order_list_item_1"></span>
                </p>
                <p >服裝類型: <span id="order_list_item_2"></span>
                </p>
           ...

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: 85vh; /* 占满整个视口高度 */
}

.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_list_container{
    position: relative;
    width: 799px;
    height: 225px;

}

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



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

.input_cust{
    margin-top: 10px;
    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 {
    background: rgb(101, 55, 5, 70%);
    color: #fafafa;
    width: 100px;
    height: 3em;
    padding: 8px;
    position: relative;
    border-radius: 2px;
 }
select:focus {
        border: none; /* Remove the border for the select element when it is focused */
      }


 .btn_container{
    position: relative;
    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;
    
  }
.order_container{
    display: none;
   ...

JavaScript

function goOrder(){
    const custName= document.getElementById('customer_name').value;
    if ((custName == '') || (custName == null)) {
        const msg = "請輸入姓名 !!" ;
        alert(msg);
        document.getElementById('customer_name').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 promotion_msg = '「最近剛好是工廠週年慶,只要您今天來電下單,'
        if (menu_1_value == 'men') {
            alert( promotion_msg+'我們將贈送您帥氣領帶!」');
        } else if  (menu_1_value == 'women'){
            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;
 
        
    };
    
};

function aboutMe(){
    alert('我們工廠位於新北市,通過國際 ISO9001 認證,品質讓您放心!');
}