1 回答
TA贡献2016条经验 获得超9个赞
在纯 JS 中如此简单:
const myForm = document.forms['my-form']
, toleR = document.getElementById('tolerance')
;
myForm.onsubmit=e=>
{
e.preventDefault() // disable form submit
let sum = Number(myForm.price1.value) + Number(myForm.price2.value)
, txt = '<p>Conservative</p>'
;
if (sum > 9) txt = '<p> Moderately Conservative </p>'
if (sum > 15) txt = '<p> Moderate </p>'
if (sum > 28) txt = '<p> Moderately Assertive </p>'
if (sum > 39) txt = '<p> Assertive </p>'
myForm.totalSum.value = sum
toleR.innerHTML = txt
}
body {
font-family: Arial, Helvetica, sans-serif;
font-size: 14px;
}
p {
display : block;
height : 1.6em;
background-color : lightgrey;
padding : .2em;
}
h3 {
margin-top: .7sem;
margin-bottom: .3em;
}
<form name="my-form" >
<h3>Sample question 1</h3>
<label> <input type="radio" value="5" name="price1" required> Answer 1 </label><br>
<label> <input type="radio" value="10" name="price1"> Answer 2 </label><br>
<label> <input type="radio" value="15" name="price1"> Answer 3 </label><br>
<label> <input type="radio" value="20" name="price1"> Answer 4 </label><br>
<h3>Sample question 2</h3>
<label> <input type="radio" value="4" name="price2" required> Answer 1 </label><br>
<label> <input type="radio" value="8" name="price2"> Answer 2 </label><br>
<label> <input type="radio" value="12" name="price2"> Answer 3 </label><br>
<label> <input type="radio" value="16" name="price2"> Answer 4 </label><br>
<br>
<button type="submit">Calculate</button>
<div id="tolerance"><p></p></div>
<h3>Your score is:</h3>
<input type="text" name="totalSum" value="" size="2" readonly >
</form>
添加回答
举报