[英]displaying text from select option with added element in the result
Guys i need a little help about this code here.伙计们,我在这里需要有关此代码的一些帮助。 So i want to display the option like this: "Fried Rice = 10.000" as the result, but in this code the text from select option box also have the price.
所以我想显示这样的选项:“炒饭 = 10.000”作为结果,但在此代码中,来自 select 选项框的文本也有价格。 For the final result i need the text from select option only have the text (without price), while after i press order button it will display text from select option name + " = " + price.
对于最终结果,我需要来自 select 选项的文本只有文本(没有价格),而在我按下订单按钮后,它将显示来自 select 选项名称+“=”+价格的文本。 Any idea what should i change?
知道我应该改变什么吗?
var data = { Food: [{ id: 1, name: 'Fried Rice', price: '10.000' }, { id: 2, name: 'Fried Noodle', price: '9.000' }, { id: 3, name: 'Pancake', price: '8.500' }, { id: 4, name: 'French Fries', price: '7.500' } ], Drink: [{ id: 1, name: 'Cola', price: '4.600' }, { id: 2, name: 'Orange Juice', price: '5.400' }, { id: 3, name: 'Mineral Water', price: '3.500' }, { id: 4, name: 'Coffee', price: '5.800' } ] } function handleChange() { var x = document.getElementById("category_select").value; var dataOptions = data[x] var dataSelect = document.getElementById('type_select') dataSelect.innerHTML = '' dataOptions.forEach(function(option) { var optionEle = document.createElement('option') optionEle.value = option.id optionEle.label = option.name + " = " + option.price dataSelect.appendChild(optionEle) }) } handleChange() $(document).ready(function() { var selectMenu = []; $("button").click(function() { selectMenu.push($("#type_select option:selected").attr('label')); $(".result").html(selectMenu.join('<br>')); }); });
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div class="container"> <div class="container-fluid text-center"> <h2 style="font-size:70px; font-family:Lucida Console;">MENU</h2> <br> <div class="row"> <div class="col-md-6"> <select class="form-select form-select-lg mb-3" id="category_select" onChange='handleChange()'> <option value="Food">Food</option> <option value="Drink">Drink</option> </select> </div> <br> <div class="col-md-6"> <select class="form-select form-select-lg mb-3" id="type_select"></select> </div> </div> </div> </div> <br> <button type="button" class="btn btn-secondary">Order</button> <br> <br> <div class="result text-center"></div>
You can add attribute
inside your option tag with name data-price
this will have price value and then show same.您可以在选项标签内添加名称为
data-price
的attribute
,这将具有价格值,然后显示相同。
Demo Code :演示代码:
var data = { Food: [{ id: 1, name: 'Fried Rice', price: '10.000' }, { id: 2, name: 'Fried Noodle', price: '9.000' }, { id: 3, name: 'Pancake', price: '8.500' }, { id: 4, name: 'French Fries', price: '7.500' } ], Drink: [{ id: 1, name: 'Cola', price: '4.600' }, { id: 2, name: 'Orange Juice', price: '5.400' }, { id: 3, name: 'Mineral Water', price: '3.500' }, { id: 4, name: 'Coffee', price: '5.800' } ] } function handleChange() { var x = document.getElementById("category_select").value; var dataOptions = data[x] var dataSelect = document.getElementById('type_select') dataSelect.innerHTML = '' dataOptions.forEach(function(option) { var optionEle = document.createElement('option') optionEle.value = option.id optionEle.setAttribute('data-price', option.price)//atr with price optionEle.label = option.name dataSelect.appendChild(optionEle) }) } handleChange() $(document).ready(function() { var selectMenu = []; $("button").click(function() { //get price and name from selected options selectMenu.push($("#type_select option:selected").attr('label') +" = "+$("#type_select option:selected").data('price')); $(".result").html(selectMenu.join('<br>')); }); });
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div class="container"> <div class="container-fluid text-center"> <h2 style="font-size:70px; font-family:Lucida Console;">MENU</h2> <br> <div class="row"> <div class="col-md-6"> <select class="form-select form-select-lg mb-3" id="category_select" onChange='handleChange()'> <option value="Food">Food</option> <option value="Drink">Drink</option> </select> </div> <br> <div class="col-md-6"> <select class="form-select form-select-lg mb-3" id="type_select"></select> </div> </div> </div> </div> <br> <button type="button" class="btn btn-secondary">Order</button> <br> <br> <div class="result text-center"></div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.