【发布时间】:2009-10-19 17:53:53
【问题描述】:
我正在尝试构建一个表单,该表单根据一系列带有字符串值的下拉框计算总价,例如“此选项花费 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>
【问题讨论】:
-
<div id="#productconfig">应该是<div id="productconfig">
标签: javascript jquery forms