javascript - 输入 key 以验证答案

标签 javascript jquery html css

我创建了一个输入框,它通过 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/

相关文章:

javascript - 方法 Set.prototype.has 调用了不兼容的接收器 undefined

javascript - Jquery UI - 在点击的div上动态绑定(bind)按键事件

javascript - 表索引和成员绑定(bind)

html - 在单行中显示文本和图像

javascript - 在 Vue 中使用 Web 组件

javascript - 检查 JavaScript 中是否存在关联数组

javascript - jQuery 找到最近的 ul

javascript - PHP/MYSQL 中自动更改选项值

javascript - Jquery - 隐藏点击文件上的任何地方

html - 使视差元素完全不透明