如何将数据价格汇总并将其插入隐藏的输入中?

发布于 2025-02-05 19:33:06 字数 1091 浏览 1 评论 0原文

我将多个无线电按钮的数据价格属性总结,然后尝试将结果插入隐藏的输入字段中。

我能够在HTML中总结span中的价格数据,但是我只是无法将其插入隐藏的输入...

我应该结合两个脚本吗?如果是这样,我该怎么做?

这是我的HTML供参考...

<input type="radio" style="display:none;" id="cdjs" data-price="90" value="2BA" name="prod">
<label for="cdjs">
  <img src="../../images/prod1.png" style="width:42px;">
  <p>Product 1</p>
</label>


<span id="price"></span>


<input type="hidden" id="hiddenprice" name="price" value=''>

这是我的脚本总结数据价格..

function calcPrice() {
  var price = 0;
  $("input[type=radio][data-price]:checked").each(function(i, el) {
    price += +$(el).data("price");
  });
  $("#price").text(price);
}

$("input[type=radio]").on("change", calcPrice);
calcPrice();

这是我的脚本,应该将结果插入隐藏的输入中。

$(function() {
  $('input').change(function() {
    var checked = $(this).find('input[type=radio]:checked');
    $('#price').html(checked.data('price'));
    $('#hiddenprice').val(checked.data('price'));
  }).change();
});

I am summing-up the data-price attribute for multiple radio buttons and then trying to insert the result into a hidden input field.

I am able to sum up the price-data within the span in my HTML but I just can't get it to insert into the hidden input...

Should I combine both scripts? and if so, how would I do this?

Here is my HTML for reference...

<input type="radio" style="display:none;" id="cdjs" data-price="90" value="2BA" name="prod">
<label for="cdjs">
  <img src="../../images/prod1.png" style="width:42px;">
  <p>Product 1</p>
</label>


<span id="price"></span>


<input type="hidden" id="hiddenprice" name="price" value=''>

Here is my script that sums up the data-price..

function calcPrice() {
  var price = 0;
  $("input[type=radio][data-price]:checked").each(function(i, el) {
    price += +$(el).data("price");
  });
  $("#price").text(price);
}

$("input[type=radio]").on("change", calcPrice);
calcPrice();

and here is my script that should insert the result into the hidden input..

$(function() {
  $('input').change(function() {
    var checked = $(this).find('input[type=radio]:checked');
    $('#price').html(checked.data('price'));
    $('#hiddenprice').val(checked.data('price'));
  }).change();
});

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文