在 JavaScript 错误警报后将焦点设置在输入字段上 [英] Set a focus on input field after alert in JavaScript error

查看:21
本文介绍了在 JavaScript 错误警报后将焦点设置在输入字段上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用 JavaScript 函数将焦点设置在特定的输入字段上.

I want to set focus on a specific input field using JavaScript function.

代码如下:

function checknag() {
  var x1 = document.getElementById('tsumnag').value;
  var x2 = document.getElementById('salenugsum').value;
  if (+x1 == +x2) {

    // here the button key 'hey' will be used as the text.
    $('#munshi_perc').focus();
  }
}

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input required class="urdu" onfocusout='checknag();' value="" type="text" />
<input required class="urdu" id="tsumnag" value="" type="text" />
<input required class="urdu" id="salenugsum" value="" type="text" />
<input required class="urdu" id="munshi_perc" value="" type="text" />

它只是专注,但我需要提醒然后专注于输入字段.

It just focuses, but I need alert and then focus on the input field.

下面的代码不起作用:

function checknag() {
  var x1 = document.getElementById('tsumnag').value;
  var x2 = document.getElementById('salenugsum').value;
  if (+x1 == +x2) {
    $.alert({
      title: 'Alert!',
      content: 'Please enter item name',
      onDestroy: function() {
        // here the button key 'hey' will be used as the text.
        $('#munshi_perc').focus();
      }
    });
    return false;
  }
}

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input required class="urdu" onfocusout='checknag();' value="" type="text" />
<input required class="urdu" id="tsumnag" value="" type="text" />
<input required class="urdu" id="salenugsum" value="" type="text" />
<input required class="urdu" id="munshi_perc" value="" type="text" />

推荐答案

来自您问题下方的评论:

From comments below your question:

我没有使用任何插件

这就是问题所在.我猜你只是从某处粘贴了一些代码,并没有查看依赖项.

So that is the problem. I guess you just pasted some code from somewhere and did not look at the dependencies.

如果加载了 jQuery-Confirm 插件,则您的代码有效.因此,只需添加以下代码段中使用的 CDN.

Your code works if the jQuery-Confirm plugin is loaded. So just add the CDNs used in the below snippet.

function checknag() {
  var x1 = document.getElementById('tsumnag').value;
  var x2 = document.getElementById('salenugsum').value;
  if (+x1 == +x2) {
    $.alert({
      title: 'Alert!',
      content: 'Please enter item name',
      onDestroy: function() {
        // here the button key 'hey' will be used as the text.
        $('#munshi_perc').focus();
      }
    });
    return false;
  }
}

<link href="https://cdnjs.cloudflare.com/ajax/libs/jquery-confirm/3.3.4/jquery-confirm.min.css" rel="stylesheet"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-confirm/3.3.4/jquery-confirm.min.js"></script>

<input required class="urdu" onfocusout='checknag();' value="" type="text" />
<input required class="urdu" id="tsumnag" value="" type="text" />
<input required class="urdu" id="salenugsum" value="" type="text" />
<input required class="urdu" id="munshi_perc" value="" type="text" />

这篇关于在 JavaScript 错误警报后将焦点设置在输入字段上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆