[英]How do you add data to an option tag?
I'd like to add a data value to an option tag in a select element.我想将数据值添加到 select 元素中的选项标签。 For example
data-price
but my problem is that I have multiple select tags.例如
data-price
但我的问题是我有多个选择标签。 So how do I get JavaScript to grab the data value of that option that the user selects?那么如何让 JavaScript 获取用户选择的那个选项的数据值呢?
How I want it to work:我希望它如何工作:
Select element #1 contains:选择元素 #1 包含:
<select onchange="updatePrice()">
<option data-price="1">Apple $1</option>
<option data-price="2">Potato $2</option>
<option data-price="3">Bag of Grapes $3</option>
</select>
Select element #2 contains:选择元素 #2 包含:
<select onchange="updatePrice()">
<option data-price="5">Really good cake</option>
<option data-price="15">Super Good Cake</option>
</select>
Then I'm honestly not sure what to do in the JS... But I want it to grab what the user selected, get the data-price
then calculate the total (just by adding select1 + select2).然后老实说,我不确定在 JS 中该做什么......但我希望它获取用户选择的内容,获取
data-price
然后计算总数(只需添加 select1 + select2)。
EDIT : My answer is different than this question because my question is more specific and requires different methods.编辑:我的回答与这个问题不同,因为我的问题更具体,需要不同的方法。 Even though this is specific it could help a developer in the future by the answers it gets.
即使这是特定的,它也可以通过它获得的答案在未来帮助开发人员。 Therefore it is not a duplicate but a more specific question than the other.
因此,它不是一个重复的问题,而是一个比另一个更具体的问题。 Though that question has a simpler answer that could be plugged in if the developer knows how to.
尽管该问题有一个更简单的答案,如果开发人员知道如何插入,则可以插入。
Here is some code matching your discription.这是一些与您的描述相符的代码。 A few notes: Use the value attribute to get direct access to the option value from the select element.
一些注意事项: 使用value属性可以从 select 元素直接访问选项值。 The unary operator (+) converts the two values from strings to a operatable numbers.
一元运算符 (+) 将两个值从字符串转换为可操作的数字。 The third div is just to show the output total value.
第三个div只是显示输出总值。
function updatePrice(){ var s1 = document.getElementById("option1"); var s2 = document.getElementById("option2"); var total = +s1.value + +s2.value; document.getElementById("price").innerHTML = "Total: $" + total // to get the text within the selected option var h1 = s1.options[s1.selectedIndex].text; return total; }
<select id="option1" onchange="updatePrice()"> <option value="1">Apple $1</option> <option value="2">Potato $2</option> <option value="3">Bag of Grapes $3</option> </select> <select id="option2" onchange="updatePrice()"> <option value="5">Really good cake</option> <option value="15">Super Good Cake</option> </select> <div id="price"></div>
Let me know if you need any further explanation.如果您需要任何进一步的解释,请告诉我。 Cheers.
干杯。
This is a bit tricky.这有点棘手。 You need to give an identifier so our code won't get confused.
您需要提供一个标识符,这样我们的代码就不会被混淆。
<select id="goods1" onchange="updatePrice(this)">
<option data-price="0">Select one</option>
<option data-price="1">Apple $1</option>
<option data-price="2">Potato $2</option>
<option data-price="3">Bag of Grapes $3</option>
</select>
<select id="goods2" onchange="updatePrice(this)">
<option data-price="0">Select one</option>
<option data-price="5">Really good cake</option>
<option data-price="15">Super Good Cake</option>
</select>
First, add a global variable to storing current price.首先,添加一个全局变量来存储当前价格。 Second, store the identifier and the price value.
其次,存储标识符和价格值。 Finally, manipulate the current price.
最后,操纵当前价格。
<script>
let price = 0;
let stored = {};
const updatePrice = elm => {
const id = elm.id;
const selectedPrice = parseInt(
Array.from(
elm.children
).filter(x => x.selected)[0].dataset.price
);
price = 0;
stored[id] = selectedPrice;
Object.keys(stored).forEach(key => price += stored[key]);
console.log(`Price: ${price}`);
};
</script>
Add some unique class to all selectbox whos valued need to be involved in total calculation like mul_ck向所有需要参与总计算的选择框添加一些独特的类,例如mul_ck
//when even a single selectbox change it will trigger re-calculation
$('.mul_ck').change(function(){
var total = 0;
//grab all _mul_ck_ and loop over them
$('.mul_ck').each(function(){
var selectValue = $(this).find(":selected").attr('data-price');
total += parseFloat(selectValue);
});
alert(total);
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.