简体   繁体   English

如何从 select 盒子到表中获取价值?

[英]How to get value from select box to table?

Good day, I am stuck at this stage and I don't have enough experience with JavaScript, in the below HTML there's select box include 2 option for phones (phone1 & phone2) and table and button called “Add”美好的一天,我被困在这个阶段,我对 JavaScript 没有足够的经验,在下面的 HTML 中有 select 框包括 2 个名为“Addphone”和 button 表的电话选项
The JavaScript code below have a function called addrow() At this moment I need to get the option I choose from the select box to the table by pressing the button “Add”下面的 JavaScript 代码有一个名为 addrow() 的 function 现在我需要通过按“Addrow()”从 select 按钮框中选择选项

eg if I choose phone1 that's mean it supposed to show in the table phone1 + price.例如,如果我选择 phone1,这意味着它应该显示在 phone1 + price 表中。 and after that if I change my mind and decide to choose phone2 that's mean will hide phone1+price and show phone2+price之后,如果我改变主意并决定选择phone2,这意味着将隐藏phone1+price并显示phone2+price

and there's “service charge” cost ($10 for both phones) it's supposed to show under phone1 and phone2并且应该在phone1和phone2下显示“服务费”成本(两部手机10美元)

eg if I choose phone1 that's mean showing in the table phone1 = first cell + price = second cell and under phone1 cell service charge = third cell and under price cell service charge cost = fourth cell例如,如果我选择 phone1,这意味着在表格中显示 phone1 = 第一个单元 + 价格 = 第二个单元,并且低于 phone1 单元服务费 = 第三个单元,并且低于价格单元服务费成本 = 第四个单元

And the same thing if I choose phone2如果我选择phone2也是一样

phone2 = first cell + price = second cell and under phone2 cell service charge = third cell and under price cell service charge cost = fourth cell phone2 = first cell + price = second cell and under phone2 cell service fee = 第三个 cell and under price cell service fee cost =第四个cell

Thanks in advance for the help.在此先感谢您的帮助。

 function addRow() { var table = document.getElementById("table"); var row = document.createElement("tr"); var td1 = document.createElement("td"); var td2 = document.createElement("td"); var td3 = document.createElement("td"); var td4 = document.createElement("td"); td1.innerHTML = document.getElementById("phone1").value; td2.innerHTML = document.getElementById("txt3").value = '$275'; td3.innerHTML = document.getElementById("phone2").value; td4.innerHTML = document.getElementById("txt4").value = '$100'; row.appendChild(td1);`enter code here` row.appendChild(td2); table.children[0].appendChild(row); }
 <select id="itemType" name="itemType"> <option id="phone1" value="phone1">phone1</option> <option id="phone2" value="phone2">phone2</option> </select> <div id="txt3"></div> <div id="txt4"></div> <input type="button" value="Add" onclick="addRow()" id="add"><br /><br /> <table id="table" border="1"> <tr> <th>Item</th> <th>Cost</th> </tr> </table>

<select id="itemType" name="itemType">
    <option id="phone1" value="phone1">phone1</option>
    <option id="phone2" value="phone2">phone2</option>
</select>

<input type="button" value="Add" onclick="addRow()" id="add"><br /><br />

<table id="table" border="1">
    <tr>
        <th>Item</th>
        <th>Cost</th>
        <th>Service Charge</th>
        <th>Total</th>
    </tr>
    <tr id="ItemDetail"></tr>
</table>

<script type="text/javascript">
    function addRow() {
        let select = document.getElementById("itemType")
        let price = 0
        let serviceCharge = 10
        if(select.value == "phone1"){
            price = 275
        }
        else if(select.value == "phone2") {
            price = 100
        }

        let data = `<td>${select.value}</td>
                    <td>$${price}</td>
                    <td>$${serviceCharge}</td>
                    <td>$${price + serviceCharge}</td>`

        document.getElementById("ItemDetail").innerHTML = select.value ? data : ''
    }
</script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM