我有一张测量表 - 宽度和高度(附图)。红色行是宽度,黄色列是高度。对于宽度400和高度700,价格是1025.最大宽度是600,最大高度是900.我用选择做了,但我有一个问题,用输入创建价格计算,如果用户可以写值在这个数字之间 - 例如,如果客户写入750 * 450mm,价格应该是1325,因为必须在700x400之后舍入到更高的价格。
jQuery('.calc-btn').click(function(e) {
e.preventDefault();
var pricePerDoor = 725,
widthInc = 225,
heightInc = 75,
oneDoorPrice,
wert1 = jQuery('#width').prop('selectedIndex') + 1,
wert2 = jQuery('#height').prop('selectedIndex') + 1,
oneDoorPrice = (pricePerDoor + (wert1 * widthInc) + (wert2 * heightInc));
jQuery('.total').val(oneDoorPrice);
});

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.1/jquery.min.js"></script>
<select id="width">
<option value="400">400mm</option>
<option value="500">500mm</option>
<option value="600">600mm</option>
</select>
<select id="height">
<option value="700">700mm</option>
<option value="800">800mm</option>
<option value="900">900mm</option>
</select>
<input type="text" name="total" class="total">
<input type="submit" class="calc-btn" value="Submit">
&#13;
答案 0 :(得分:2)
您可以从最近的最高数字格式数组的宽度和高度array
索引进行检查,并为您指定逻辑索引。
for (var i = widthArr.length - 1; i >= 0; i--) {
if (widthArr[i] >= wert1)
widIndex = i + 1;
}
工作样本
jQuery('.calc-btn-input').click(function(e) {
e.preventDefault();
var pricePerDoor = 725,
widthInc = 225,
heightInc = 75;
var widthArr = [400, 500, 600];
var heightArr = [700, 800, 900];
var widIndex = widthArr.length;
var heightIndex = heightArr.length;
var wert1 = parseInt(jQuery('#width_input').val());
var wert2 = parseInt(jQuery('#height_input').val());
for (var i = widthArr.length - 1; i >= 0; i--) {
if (widthArr[i] >= wert1)
widIndex = i + 1;
}
for (var i = heightArr.length - 1; i >= 0; i--) {
if (heightArr[i] >= wert2)
heightIndex = i + 1;
}
var oneDoorPrice = (pricePerDoor + (widIndex * widthInc) + (heightIndex * heightInc));
jQuery('.total-input').val(oneDoorPrice);
});
jQuery('.calc-btn').click(function(e) {
e.preventDefault();
var pricePerDoor = 725,
widthInc = 225,
heightInc = 75,
oneDoorPrice,
wert1 = jQuery('#width').prop('selectedIndex') + 1,
wert2 = jQuery('#height').prop('selectedIndex') + 1,
oneDoorPrice = (pricePerDoor + (wert1 * widthInc) + (wert2 * heightInc));
jQuery('.total').val(oneDoorPrice);
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
<select id="width">
<option value="400">400mm</option>
<option value="500">500mm</option>
<option value="600">600mm</option>
</select>
<select id="height">
<option value="700">700mm</option>
<option value="800">800mm</option>
<option value="900">900mm</option>
</select>
<input type="text" name="total" class="total">
<input type="submit" class="calc-btn" value="Submit">
<br>
<br>
<input id="width_input">
<input id="height_input">
<input type="text" name="total" class="total-input">
<input type="submit" class="calc-btn-input" value="Submit">
&#13;