Set a focus on input field after alert in JavaScript error

萝らか妹 提交于 2021-02-17 07:15:44

问题


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

Here is the coding:

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.


Update

The code below does not work:

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" />

回答1:


From comments below your question:

no im not using any plugin

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

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" />


来源:https://stackoverflow.com/questions/65689254/set-a-focus-on-input-field-after-alert-in-javascript-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!