我在预订表格上工作,该表格显示营地的总费用,然后再确认。我的问题是我可以编写一个脚本来显示表单的外观,但它不起作用。
这是我的剧本:
HTML
<td>Student</td>
<td>
<select name="stu" id="Student">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
</select>
</td>
<td>Teacher</td>
<td>
<select name="tchr" id="Teacher">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
</select>
</td>
<td>House Type</td>
<td>
<input type="radio" name="accom" value="100" id="a1">
<label for="a1">Hotel</label>
<input type="radio" name="accom" value="0" id="a2">
<label for="a2">House</label>
</td>
<td>Accommodation</td>
<td>
<span id="zStay">0</span>
</td>
<td>Total</td>
<td>
<span id="total1">0</span>
</td>
Javascript
$(window).load(function () {
var nTchr = 0;
var nStu = 0;
var nStay = 0;
$("#Teacher").change(function () {
nTchr = parseInt($(this).val(), 10);
calcTotals();
});
$("#Student").change(function () {
nStu = parseInt($(this).val(), 10);
calcTotals();
});
$('input[name=accom]').change(function (evt) {
nStay = parseInt($(this).val(), 10);
calcTotals();
});
function calcTotals() {
var zStay = nStay * (nStu + nTchr);
$('#zStay').number(zStay);
$("#total01").number(zStay + 60 * nTchr + 150 * nStu);
$("#total1").val(zStay + 60 * nTchr + 150 * nStu);
}
});
其中$.number()
是插件,供参考:
$.fn.number = function (number, decimals, dec_point, thousands_sep) {
number = (number + '').replace(/[^0-9+\-Ee.]/g, '');
var n = !isFinite(+number) ? 0 : +number,
prec = !isFinite(+decimals) ? 0 : Math.abs(decimals),
sep = (typeof thousands_sep === 'undefined') ? ',' : thousands_sep,
dec = (typeof dec_point === 'undefined') ? '.' : dec_point,
s = '',
toFixedFix = function (n, prec) {
var k = Math.pow(10, prec);
return '' + Math.round(n * k) / k;
};
// Fix for IE parseFloat(0.55).toFixed(0) = 0;
s = (prec ? toFixedFix(n, prec) : '' + Math.round(n)).split('.');
if (s[0].length > 3) {
s[0] = s[0].replace(/\B(?=(?:\d{3})+(?!\d))/g, sep);
}
if ((s[1] || '').length > prec) {
s[1] = s[1] || '';
s[1] += new Array(prec - s[1].length + 1).join('0');
}
// Add this number to the element as text.
this.text(s.join(dec));
};
&#13;
在这里演示:http://jsfiddle.net/8u26ozh3/
直白:
答案 0 :(得分:0)
你应该替换:
$(window).load(function () {
使用:
$(document).ready(function () {
答案 1 :(得分:0)
答案 2 :(得分:0)
请更正你的代码,我得到了输出
<html>
<head>
<meta charset="utf-8">
<link rel="stylesheet" href="//code.jquery.com/ui/1.10.4/themes/smoothness/jquery-ui.css">
<script src="//code.jquery.com/jquery-1.9.1.js"></script>
<script src="//code.jquery.com/ui/1.10.4/jquery-ui.js"></script>
</head>
<body id="contextmenu">
<form>
<table border width="100%">
<tr>
<td>Student</td>
<td>
<select name="stu" id="Student">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
</select>
</td>
</tr>
<tr>
<td>Teacher</td>
<td>
<select name="tchr" id="Teacher">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
</select>
</td>
</tr>
<tr>
<td>House Type</td>
<td>
<input type="radio" name="accom" value="100" id="a1">
<label for="a1">Hotel</label>
<input type="radio" name="accom" value="0" id="a2">
<label for="a2">House</label>
</td>
</tr>
<tr>
<td>Accommodation</td>
<td>
<span id="zStay">0</span>
</td>
</tr>
<tr>
<td>Total</td>
<td>
<span id="total1">0</span>
</td>
</tr>
</table>
</form>
<script lang=JavaScript>
$.fn.number = function (number, decimals, dec_point, thousands_sep) {
number = (number + '').replace(/[^0-9+\-Ee.]/g, '');
var n = !isFinite(+number) ? 0 : +number,
prec = !isFinite(+decimals) ? 0 : Math.abs(decimals),
sep = (typeof thousands_sep === 'undefined') ? ',' : thousands_sep,
dec = (typeof dec_point === 'undefined') ? '.' : dec_point,
s = '',
toFixedFix = function (n, prec) {
var k = Math.pow(10, prec);
return '' + Math.round(n * k) / k;
};
// Fix for IE parseFloat(0.55).toFixed(0) = 0;
s = (prec ? toFixedFix(n, prec) : '' + Math.round(n)).split('.');
if (s[0].length > 3) {
s[0] = s[0].replace(/\B(?=(?:\d{3})+(?!\d))/g, sep);
}
if ((s[1] || '').length > prec) {
s[1] = s[1] || '';
s[1] += new Array(prec - s[1].length + 1).join('0');
}
// Add this number to the element as text.
this.text(s.join(dec));
};
$(window).load(function () {
var nTchr = 0;
var nStu = 0;
var nStay = 0;
$("#Teacher").on('change', function () {
nTchr = parseInt($(this).val(), 10);
calcTotals();
});
$("#Student").on('change', function () {
nStu = parseInt($(this).val(), 10);
calcTotals();
});
$('input[name=accom]').on('change', function (evt) {
nStay = parseInt($(this).val(), 10);
calcTotals();
});
function calcTotals() {
var zStay =parseInt(nStay * (nStu + nTchr));
$('#zStay').text(zStay);
$("#total01").text(zStay + 60 * nTchr + 150 * nStu);
$("#total1").text(zStay + 60 * nTchr + 150 * nStu);
}
}); //]]>
</script>
</body>
</html>