gpt4 book ai didi

javascript - 在 jQuery 中使用 Each() 和 Children() 形成计算器

转载 作者:行者123 更新时间:2023-11-29 22:42:40 24 4
gpt4 key购买 nike

我正在尝试构建一个表格,该表格基于一系列带有字符串值的下拉框来计算总价,例如“此选项成本 30 英镑”,我知道这并不理想,但我将其放在一起作为一个 hack现有脚本

在大多数情况下,我都能正常工作,但我不确定如何为#productconfig 的每个子项运行每个函数

我可以手动将每个下拉列表的 ID 输入到一个数组中,然后进行计算,但如果它只与 #productconfig 的所有子项一起使用会很好

<code>

<div id="#productconfig">
<label>Model Type</label>
<select name="products[220][data][modeltype]" id="data-modeltype-220">
<option value="M-Type £500">M-Type £500</option>
<option value="P-Type £500">P-Type £500</option>
<option value="S-Type £500">S-Type £500</option>
</select>
</div>
</code>

<code>
$(document).ready(function() {
$("#productconfig").children().change(function () {
calculateoptions();
});
calculateoptions();
});
</code>
<code>
function calculateoptions() {

var arr = ["data-modeltype-220"];

var total = 0;

jQuery.each(arr, function () {
var str = $('#' + this).attr("value");
var poundsign = str.indexOf('£');
var poundsign = poundsign + 1;
var lengthofstr = str.length;
var shortstr = str.substr(poundsign, lengthofstr);
total = eval(total) + eval(shortstr);
});

$('#price').html("£" + total);

}
</code>

最佳答案

这个怎么样:

function calculateoptions() {

var total = 0;

jQuery('#productconfig select').each(function () {
total += $(this).val().match(/£(\d+)/)[1];
});

$('#price').html("£" + total);

}

关于javascript - 在 jQuery 中使用 Each() 和 Children() 形成计算器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1590134/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com