如何将光标聚焦在文本框上? [英] how to focus cursor on textbox?

查看:143
本文介绍了如何将光标聚焦在文本框上?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我在javascript中用于验证textBox是否为空的代码,
它可以正常工作,但是在验证之后它会跳转到下一个文本框,我希望我的光标指向同一个文本框,直到他输入正确的输入为止.

This is my code in javascript to validate whether the textBox is empty or not,
it works fine but after validating it jumps to next textbox, I want my cursor to be pointing on the same textBox untill he enters the correct input.

function trainNoName(txt)
{
  var trainNo=txt.value;
  if(trainNo=="")
  {
  alert(txt.name+" Field Can''t be empty");
  document.getElementById("stFrm").focus();
  }
}




//我在最后一行中使用了focus(),但它不起作用.




//i have used focus() in my last line but its not working.

推荐答案

尝试以下操作:-

Try this:-

function trainNoName(txt)
{
  var txtTN=document.getElementById(txt);
  var trainNo=txtTN.value;
  if(trainNo==''''){
  alert(''Train No Field Cannot be empty'');
  document.getElementById(txtTN).focus();
  }
}



我希望这能解决问题.



I hope this will work.


用你的思想.


使用if()... else....
Use your mind.


use if()...else....


我将假定txt是输入,因为您正在访问txt.valuetxt.name:
I''ll assume that txt is the input, since you are accessing txt.value and txt.name:
function trainNoName(txt) {
   var trainNo=txt.value;
   if(trainNo=="") {
      alert(txt.name+" Field Can't be empty");
      txt.focus();
   }
}


这篇关于如何将光标聚焦在文本框上?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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