参考答案:
满足第一题,显示图片,单价默认10元,运费默认12元
<form action="" method="">
<table border="1px" cellspacing="" cellpadding="">
<tr>
<th colspan="2">在线购物</th>
</tr>
<tr>
<td colspan="2"><img src="./AJ1.webp" width="200"></td>
</tr>
<tr>
<td>单价:</td>
<td><input type="text" value="10">元</td>
</tr>
<tr>
<td>数量:</td>
<td><input type="text">件</td>
</tr>
<tr>
<td>运费:</td>
<td><input type="text" value="12">元</td>
</tr>
<tr>
<td><input type="button" value="结算"></td>
<td><input type="text">元</td>
</tr>
</table>
</form>
满足第二题,可以实现基本运算
//HTML部分不变,在下方添加JavaScript代码块即可
<script type="text/javascript">
function jisuan() {
price = parseFloat(document.getElementById('danjia').value);
amount = parseFloat(document.getElementById('shuliang').value);
post = parseFloat(document.getElementById('yunfei').value);
var sum = price * amount;
if (sum < 88) {
sum += post;
}
document.getElementById('jieguo').value = sum;
}
</script>
满足第三题,实现进阶运算
//HTML部分
<tr>
<td>支付方式:</td>
<td><select name="zhifu" id="zhifu">
<option value="1">现金</option>
<option value="2">银行卡</option>
<option value="3">会员卡</option>
<option value="4">支付宝/微信</option>
</select></td>
</tr>
//JavaScript部分
function jisuan() {
price = parseFloat(document.getElementById('danjia').value);
amount = parseFloat(document.getElementById('shuliang').value);
post = parseFloat(document.getElementById('yunfei').value);
var sum = price * amount;
if (sum < 88) {
sum += post;
}
document.getElementById('jieguo').value = sum * discount();
}
function discount() {
var zhekou = parseInt(document.getElementById('zhifu').value);
switch (zhekou) {
case 1:
return 1;
break;
case 2:
case 4:
return 0.95;
break;
case 3:
return 0.8;
break;
default:
break;
}
}
此时已经满足测试要求,以下提升按需使用。
添加两行代码,已实现包邮时运费显示0元,不满足包邮条件显示12元。
function jisuan() {
price = parseFloat(document.getElementById('danjia').value);
amount = parseFloat(document.getElementById('shuliang').value);
post = parseFloat(document.getElementById('yunfei').value);
var sum = price * amount;
if (sum < 88) {
document.getElementById('yunfei').value = "12";
sum += post;
} else {
document.getElementById('yunfei').value = "0";
}
document.getElementById('jieguo').value = sum * discount();
}
最后实现效果