我创建了一个输入框,它通过 Tab 键验证。但需要在按下回车按钮时进行验证。
function checktxt(h, v, w, c) {
var th = $("#" + h).val();
var tv = $("#" + v).val();
if (th.toLowerCase() == tv.toLowerCase()) {
$("." + c).show();
$("." + w).hide();
var win = new Audio('audio/wright.mp3');
} else if (tv.toLowerCase() == "") {
} else {
$("." + c).hide();
$("." + w).show();
//setTimeout(function() { $( "."+w ).hide(); }, 5000);
var win = new Audio('audio/wrong.mp3');
}
win.play();
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div style="position:absolute; margin-left:350px;top:226px;">
<input id="texthidden1" type="hidden" value="413" />
<input id="textvisible1" type="text" value="" style="font-size:32px;text-align:center;border:0px solid black;height:50px;width:100px;" maxlength="4" onblur="javascript:checktxt('texthidden1','textvisible1','wrong1','correct1');" />
<div class="wrong1" style="margin-left: 120px;margin-top: -30px; position:absolute; float:left; display: none; margin-bottom:-30px; ">
<img src="../images/smallwrong.png" />
</div>
<div class="correct1" style="position:absolute;margin-left: 120px; margin-top: -30px; float:left; display: none; margin-bottom:-30px;">
<img src="../images/smallgreen.png" />
</div>
</div>
最佳答案
您可以在检测到按下回车键时执行验证。
$(document).keypress(function(e) {
if(e.which == 13) {
// enter pressed now validate
checktxt(h,v,w,c)
}
});
关于javascript - 输入 key 以验证答案,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41956616/