[英]Get dataset value from input tag upon click and display as HTML
I would like to get the value from the data-price dataset in the radio button after the user has clicked a different option and display it below in the label tag. 我想在用户单击其他选项并将其显示在下面的标签之后,从单选按钮的数据价格数据集中获取值。 I am doing something wrong. 我做错了。 Any help would be very much appreciated. 任何帮助将不胜感激。
Here is my code: 这是我的代码:
(function (){ var radios_price = document.getElementById('lbl-011'); console.log(radios_price.dataset.price); for(var i = 0; i < radios_price.length; i++){ radios_price[i].onclick = function(){ document.getElementById('priceLabel').innerText = this.value; } } })();
<input id="lbl-011" class="click_radio checkbox123" data-name="One Gel Pen with One Blue Light" name="product_quantity" data-val="2264" data-price="29" data-qty="1" value="1" type="radio"> <input id="lbl-011" class="click_radio" data-name="Two Gel Pens and One Blue Light" name="product_quantity" data-val="2263" data-price="39" data-qty="2" value="2" type="radio" checked="checked"> <input id="lbl-011" class="click_radio" data-name="Four Gel Pens and One Blue Light with Bonus Mouth Tray" name="product_quantity" data-val="2259" data-qty="4" data-price="49" value="4" type="radio"> <p>Price (data-price):<label id="priceLabel"> $39</label>
You can't have an id
assigned to more than one element. 您不能将id
分配给多个元素。 Lots of tutorials always put a descriptive text as both id
and name
attribute, and in the case of radio buttons you do need them all to have the same name
, but the id
must be unique to an element. 许多教程总是将描述性文本同时作为id
和name
属性,并且对于单选按钮,您确实需要它们都具有相同的name
,但是id
必须是元素唯一的。
(function (){ document.querySelectorAll('#lbl-011 input').forEach((inp) => { inp.onclick = function () { document.getElementById('priceLabel').innerText = "$" + this.dataset.price; }; }); })();
<div id="lbl-011"> <input class="click_radio checkbox123" data-name="One Gel Pen with One Blue Light" name="product_quantity" data-val="2264" data-price="29" data-qty="1" value="1" type="radio"> <input class="click_radio" data-name="Two Gel Pens and One Blue Light" name="product_quantity" data-val="2263" data-price="39" data-qty="2" value="2" type="radio" checked="checked"> <input class="click_radio" data-name="Four Gel Pens and One Blue Light with Bonus Mouth Tray" name="product_quantity" data-val="2259" data-qty="4" data-price="49" value="4" type="radio"> </div> <p>Price (data-price): <span id="priceLabel">$39</span>
I also turned the <label>
into a <span>
and move the whitespace outside of it. 我还把<label>
变成了<span>
,并将空格移到了它外面。
As pointed out, it's important not to assign the same ID to multiple elements. 如前所述,重要的是不要将相同的ID分配给多个元素。 That is what classes are used for. 这就是类的用途。
This is also a good case for taking advantage of event delegation. 这也是利用事件委派的好案例。 If you wrap all your input elements in a container you can create an event listener there and respond to whichever radio button was clicked. 如果将所有输入元素包装在容器中,则可以在其中创建事件侦听器,并响应单击的单选按钮。 Doing this you also want to check and ensure that it was actually a radio button that was clicked, and not some other empty space in the parent element. 为此,您还需要检查并确保它实际上是被单击的单选按钮,而不是父元素中的其他一些空白区域。
(function (){ var inputs = document.getElementById('inputCollection'); inputs.addEventListener('click', function(e){ console.log('Collection Clicked'); if(e.target instanceof HTMLInputElement) { // e.target will be the specific input button clicked document.getElementById('priceLabel').innerHTML = ' $' + e.target.dataset.price; } }) })();
<div id="inputCollection"> <input id="lbl-011" class="click_radio checkbox123" data-name="One Gel Pen with One Blue Light" name="product_quantity" data-val="2264" data-price="29" data-qty="1" value="1" type="radio"> <input id="lbl-012" class="click_radio" data-name="Two Gel Pens and One Blue Light" name="product_quantity" data-val="2263" data-price="39" data-qty="2" value="2" type="radio" checked="checked"> <input id="lbl-013" class="click_radio" data-name="Four Gel Pens and One Blue Light with Bonus Mouth Tray" name="product_quantity" data-val="2259" data-qty="4" data-price="49" value="4" type="radio"> </div> <p>Price (data-price):<label id="priceLabel"> $39</label>
You can collect all input type with help of classname, call in for loop to access each element. 您可以在类名的帮助下收集所有输入类型,并调用for循环访问每个元素。
(function (){ var radios_price = document.getElementsByClassName('click_radio'); for(var i = 0; i < radios_price.length; i++){ radios_price[i].onclick = function(){ document.getElementById('priceLabel').innerText = "$"+this.dataset.price; } } })();
<input id="lbl-011" class="click_radio checkbox123" data-name="One Gel Pen with One Blue Light" name="product_quantity" data-val="2264" data-price="29" data-qty="1" value="1" type="radio"> <input id="lbl-011" class="click_radio" data-name="Two Gel Pens and One Blue Light" name="product_quantity" data-val="2263" data-price="39" data-qty="2" value="2" type="radio" checked="checked"> <input id="lbl-011" class="click_radio" data-name="Four Gel Pens and One Blue Light with Bonus Mouth Tray" name="product_quantity" data-val="2259" data-qty="4" data-price="49" value="4" type="radio"> <p>Price (data-price):<label id="priceLabel">$39</label>
This should give you the desired output. 这将为您提供所需的输出。 Like others said, the ID has to be unique. 就像其他人所说的,ID必须是唯一的。 Only the class can be the same. 只有类别可以相同。
(function() {
let radios = document.querySelectorAll('.click_radio');
radios.forEach((radio) => {
radio.addEventListener('change', () => {
// This gives you access to all the data attributes you've declared
//
let radioData = radio.dataset;
document.getElementById('priceLabel').innerText = `$${ radioData.price }`
})
})
})()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.