如何将数据价格汇总并将其插入隐藏的输入中?
我将多个无线电按钮的数据价格属性总结,然后尝试将结果插入隐藏的输入字段中。
我能够在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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论