我正在尝试在输入字段中输入错误的值时打印错误消息,但我不知道如何执行此操作。如果随机值为6且用户键入7,则会显示一条用户消息弹出窗口,显示“您输入错误”。
function RandomNum(min, max) {
return parseInt(Math.random() * (max - min) + min);
}
let key1 = RandomNum(1, 10)
let key2 = RandomNum(1, 10)
$("#rnd1").html(key1.toString() + " + " + key2.toString())
$("#submit").click(() => {
alert(parseInt($("#rnd2").val()) === (key1 + key2))
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<p>Human test <span id="rnd1"></span></p>
<form>
<input type="text" id="rnd2">
<input type="submit" id="submit" value="validate">
</form>
答案 0 :(得分:0)
我认为这就是你所需要的。
function RandomNum(min, max) {
return parseInt(Math.random() * (max - min) + min);
}
let key1 = RandomNum(1, 10)
let key2 = RandomNum(1, 10)
$("#rnd1").html(key1.toString() + " + " + key2.toString())
$("#rnd2").on("keyup", (e) => {
$("#error").hide();
if(parseInt($("#rnd2").val()) !== (key1 + key2)){
$("#error").show();
}
else{
$("#error").hide();
$("#not-error").show();
}
})
#error{
position : absolute;
bottom: 0;
background : red;
display : none
}
#not-error{
position : absolute;
bottom: 0;
background : green;
display : none
color : white
}
#box{
position : relative;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form>
<div id="box">
<input type="text" id="rnd2">
<span id="error">You have wrong entry</span>
<span id="not-error">valid</span>
</div>
<input type="submit" id="submit" value="validate">
</form>
<p>Human test <span id="rnd1"></span></p>