我已经制作了一个简单的表单,其中有一个是和否的复选框,我想要它,这样当我按下它时会在一个警告框中显示5,当我按下它时它会显示0但是当前我点击提交它在警报框中显示[Object htmlDivElement]。
<script>
function alertprice() { // this will be called on submit of the form
alert (runningtotal); // alert the value in variable running total
}
function totalprice () { // function totalprice will set runningtotal depending on boxes ticked
if(document.getElementById('ramyes').checked) {
var runningtotal = "5"
return runningtotal;
}else if(document.getElementById('ramno').checked) {
var runningtotal = "0"
return runningtotal;
}
}
</script>
和html
<form name="theForm" action="shop.html" method="post" onSubmit="checkWholeForm(theForm); return alertprice()">
<h2>Upgrade RAM to "4GB-Kit GEIL Evo One PC3-12800 DDR3-1600 CL9"?</h2>
<h3>
<input type="radio" name="ram" value="yes" id="ramyes">yes<br>
<input type="radio" name="ram" value="no" id="ramno">no
</h3>
答案 0 :(得分:2)
你必须调用你的函数totalprice()。例如:
function alertprice(){
alert(totalprice());
}