我的家庭作业是像聚会一样进行活动,然后给出四个年龄段的价格。用户应该输入他们的年龄,并且它将告诉他们所支付的价格。我还想告诉用户是否输入了错误的数字,例如0或1000或一个单词,但是由于我从未教过怎么做,所以我还没有弄清楚该怎么做。
<form id= "age">
<p><input type= "text" value= "Enter age" id= "text1"></p>
<p><button class="w3-button w3-black" type="button" onclick= "message1" id= "button">Amount Due</button></p>
</form>
</div>
</div>
</div>
</div>
<script>
var age
function message1 () {
if (age >= 0 && age => 12) {
window.alert("You are not eligible to go to this event")
} if ( age => 13 && age => 18) {
window.alert("Your amount due is $5.00")
} if (age => 19 && age => 24) {
window.alert("Your amount due is $7.00")
} if (age => 25 && age => 54) {
window.alert("Your amount due is $9.00")
} if (age => 55 && age => 99) {
window.alert("Your amount due is $11.00")
}
}
</script>
答案 0 :(得分:0)
获得灵感的快速答案:
const formAge = document.getElementById('form-Age')
formAge.onsubmit = function(evt)
{
evt.preventDefault()
alert( message1(formAge.age.valueAsNumber))
}
function message1(age)
{
if (age < 13) return "You are not eligible to go to this event"
if (age < 19) return "Your amount due is $5.00"
if (age < 25) return "Your amount due is $7.00"
if (age < 55) return "Your amount due is $9.00"
return "Your amount due is $11.00"
}
<form id="form-Age">
<p>Enter age : <input type="number" name="age" min="0" max="99" value="0"></p>
<p><button class="w3-button w3-black" type="submit">Amount Due</button></p>
</form>
答案 1 :(得分:0)
函数名称需要使用方括号。
<form id= "age">
<p><input type= "number" min="1" max="150" value= "Enter age" id= "text1"></p>
<p><button class="w3-button w3-black" type="button" onclick= "message1()" id= "button">Amount Due</button></p>
</form>
只需使用基于id的jQuery选择器获取值。 使用<=小于等于。 并使用Alert而不是window.alert。 在每个语句后使用分号。
如果您想开始进行Web开发,请点击here!
<script>
function message1 () {
var age = $("#text1").val();
if (age >= 0 && age <= 12) {
alert("You are not eligible to go to this event");
} if ( age >= 13 && age <= 18) {
alert("Your amount due is $5.00");
} if (age >= 19 && age <= 24) {
alert("Your amount due is $7.00");
} if (age >= 25 && age <= 54) {
alert("Your amount due is $9.00");
} if (age >= 55 && age <= 99) {
alert("Your amount due is $11.00");
}
}
</script>