Jquery加价的酒店和房间计算价格
Jquery calculation price of hotel and room with additional price
演示:http://jsfiddle.net/sando779/16wws4w1/
<select name="one" id="one">
<option value="0">Select Hotel</option>
<option value="3000">Nirvana</option>
<option value="6000">Laguna</option>
<option value="9000">Palm beach</option>
</select>
<br />
<br />
<select name="two" id="two">
<option>Room Type</option>
</select>
<br/>
<br/>
<p>
<input type="checkbox" name="tri" id="tri" value="50"/>
<label >additional Price</label>
</p>
<div id="total"></div>
// arrays instead of comma separated list and added base key
var data = {
"0": ["Select Room type"],
"3000": ["economist_0", "executive_465", "businesse_984"],
"6000": ["economist_200", "executive_700", "businesse_800"],
"9000": ["economist_400", "executive_800", "businesse_900"]
}
$("#one").change(function () {
var first = $(this),
second = $("#two"),
third = $("#tri"),
key = first.val(),
// instead of the original switch code
vals = data[key] == undefined ? data.base : data[key],
html = [];
// create insert html before adding
$.each(vals, function (i, val) {
var v = val.split('_');
html.push('<option value="' + v[1] + '">' + v[0] + '</option>')
});
// no need to empty the element before adding the new content
second.html(html.join());
});
$("#one,#two,#tri").change(function () {
var val1 = parseInt($('#one').val()) || 0,
val2 = parseInt($('#two').val()) || 0,
val3 = parseInt($('#tri').val()) || 0;
$('#total').text(val1 + val2 + val3)
})
我想对不同类型表单元素的值求和。下拉输入。我卡在添加额外价格后总计不是自动更新
即使未选中 checkbox
,您也在添加附加值。如果选中 checkbox
,您只需添加附加值。你可以试试这个 jsfiddle http://jsfiddle.net/16wws4w1/1/
演示:http://jsfiddle.net/sando779/16wws4w1/
<select name="one" id="one">
<option value="0">Select Hotel</option>
<option value="3000">Nirvana</option>
<option value="6000">Laguna</option>
<option value="9000">Palm beach</option>
</select>
<br />
<br />
<select name="two" id="two">
<option>Room Type</option>
</select>
<br/>
<br/>
<p>
<input type="checkbox" name="tri" id="tri" value="50"/>
<label >additional Price</label>
</p>
<div id="total"></div>
// arrays instead of comma separated list and added base key
var data = {
"0": ["Select Room type"],
"3000": ["economist_0", "executive_465", "businesse_984"],
"6000": ["economist_200", "executive_700", "businesse_800"],
"9000": ["economist_400", "executive_800", "businesse_900"]
}
$("#one").change(function () {
var first = $(this),
second = $("#two"),
third = $("#tri"),
key = first.val(),
// instead of the original switch code
vals = data[key] == undefined ? data.base : data[key],
html = [];
// create insert html before adding
$.each(vals, function (i, val) {
var v = val.split('_');
html.push('<option value="' + v[1] + '">' + v[0] + '</option>')
});
// no need to empty the element before adding the new content
second.html(html.join());
});
$("#one,#two,#tri").change(function () {
var val1 = parseInt($('#one').val()) || 0,
val2 = parseInt($('#two').val()) || 0,
val3 = parseInt($('#tri').val()) || 0;
$('#total').text(val1 + val2 + val3)
})
我想对不同类型表单元素的值求和。下拉输入。我卡在添加额外价格后总计不是自动更新
即使未选中 checkbox
,您也在添加附加值。如果选中 checkbox
,您只需添加附加值。你可以试试这个 jsfiddle http://jsfiddle.net/16wws4w1/1/