Javascript 汇总多个选择框的值
我试图从多个选择框中总结出一些值。问题是这些选项同时包含文本和数字。我只需要这些数字,然后把它们加起来。没有其他方法可以做到这一点。 因此,我尝试的是从字符串中删除文本,然后在每次selectbox更改时更新数量。现在的结果总是错误的 我的另一个问题是,有时数字前面有一个负号。有没有办法从总数中提取出来?在小提琴的第三个选择框中有一个例子 有人能帮忙吗?我创造了一个 HTMLJavascript 汇总多个选择框的值,javascript,jquery,Javascript,Jquery,我试图从多个选择框中总结出一些值。问题是这些选项同时包含文本和数字。我只需要这些数字,然后把它们加起来。没有其他方法可以做到这一点。 因此,我尝试的是从字符串中删除文本,然后在每次selectbox更改时更新数量。现在的结果总是错误的 我的另一个问题是,有时数字前面有一个负号。有没有办法从总数中提取出来?在小提琴的第三个选择框中有一个例子 有人能帮忙吗?我创造了一个 HTML 如果不想将这些数字放入值中,则可以使用。 因此,您可以通过以下方式对选择框进行编码: <select id="my
如果不想将这些数字放入值中,则可以使用。
因此,您可以通过以下方式对选择框进行编码:
<select id="mySelectBox">
<!--selected as an example-->
<option value="123" data-price="27000" selected>Some Stuff ($27,000)</option>
<option value="124" data-price="12000">Magic Sword ($12,000)</option>
</select>
如果不想将这些数字放入值中,则可以使用。
因此,您可以通过以下方式对选择框进行编码:
<select id="mySelectBox">
<!--selected as an example-->
<option value="123" data-price="27000" selected>Some Stuff ($27,000)</option>
<option value="124" data-price="12000">Magic Sword ($12,000)</option>
</select>
假设您根本无法更改HTML结构,并且最后一个括号将包含价格(如果有):
function update_amounts() {
var sum = 0.0;
$('#product_configure_form .product-configure-custom-option select').each(function () {
var optionText = $(this).find('option:selected').text();
var matches = optionText.match(/\(([^)]+)\)/g) || [];
if (matches.length > 0) {
var priceText = matches[matches.length - 1];
if (priceText.indexOf("€") > -1) {
var priceClean = priceText.replace(/[^0-9\+\-\.\,]/g, '');
var priceValue = parseFloat(priceClean) || 0;
sum += priceValue;
}
}
});
$('#amount').text('€' + sum.toFixed(2));
}
$( "#product_configure_form").on("change", ".product-configure-custom-option select", update_amounts);
您可能需要根据小数点字符调整“priceClean”正则表达式。假设您根本无法更改HTML结构,并且最后一个括号将包含价格(如果有):
function update_amounts() {
var sum = 0.0;
$('#product_configure_form .product-configure-custom-option select').each(function () {
var optionText = $(this).find('option:selected').text();
var matches = optionText.match(/\(([^)]+)\)/g) || [];
if (matches.length > 0) {
var priceText = matches[matches.length - 1];
if (priceText.indexOf("€") > -1) {
var priceClean = priceText.replace(/[^0-9\+\-\.\,]/g, '');
var priceValue = parseFloat(priceClean) || 0;
sum += priceValue;
}
}
});
$('#amount').text('€' + sum.toFixed(2));
}
$( "#product_configure_form").on("change", ".product-configure-custom-option select", update_amounts);
您可能需要根据小数点字符调整“priceClean”正则表达式。将数字放在
值属性中,并使用该属性,而不是文本。您能否详细说明为什么“没有其他方法可以做到这一点”?限制条件是什么?将数字放在值
属性中,并使用该属性,而不是文本。您能否详细说明为什么“没有其他方法可以做到这一点”?限制是什么?这正是我的意思。我无法更改html。。。此解决方案的thx!!这正是我的意思。我无法更改html。。。此解决方案的thx!!这将是我的正常方式,但我无法更改代码中的任何内容。这将是我的正常方式,但我无法更改代码中的任何内容。
function update_amounts() {
var sum = 0.0;
$('#product_configure_form .product-configure-custom-option select').each(function () {
var optionText = $(this).find('option:selected').text();
var matches = optionText.match(/\(([^)]+)\)/g) || [];
if (matches.length > 0) {
var priceText = matches[matches.length - 1];
if (priceText.indexOf("€") > -1) {
var priceClean = priceText.replace(/[^0-9\+\-\.\,]/g, '');
var priceValue = parseFloat(priceClean) || 0;
sum += priceValue;
}
}
});
$('#amount').text('€' + sum.toFixed(2));
}
$( "#product_configure_form").on("change", ".product-configure-custom-option select", update_amounts);