使用javascript在asp.net的Textfield上设置Focous [英] Set Focous on a Textfield in asp.net using javascript

查看:94
本文介绍了使用javascript在asp.net的Textfield上设置Focous的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用JavaScript在asp.net上的注册表单上进行一些验证。但它没有按我想要的方式工作。我希望如果用户将文本字段留空,则会弹出一条警告消息,然后设置强制用户使用焦点填充该字段。



我写这段代码,但问题是在显示警告消息后,它没有将焦点设置在TextBox2上。



< asp:TextBox ID =   TextBox2 runat =   server onblur =   ValidateTextNotNull() < span class =code-keyword>>  < /   asp:TextBox  < span class =code-keyword>>  





  function  ValidateTextNotNull()
{
var Firstname = 文件 .getElementById(' TextBox2')。value;
var textLength = Firstname.length;
if (textLength == 0 ){
alert( 显示您的消息);
document .getElementById(' TextBox2'< /跨度>)聚焦();
}

}

解决方案

替换 document.getElementById(' TextBox2') with document.getElementById('<%= TextBox2.ClientID%>')



请阅读文档 [ ^ ]



引用:

当Web服务器控件呈现为HTML元素时,HTML元素的id属性设置为ClientID属性的值。 ClientID值通常用于通过使用document.getElementById方法访问客户端脚本中的HTML元素。


I am trying to put some validation on my registration form in asp.net using JavaScript. but it didn't work the way i want. i want that if user leave a text-field empty an alert message pops up and then set force user to fill that field by using focus.

I write this code, but the problem is that after showing alert message, it didn't set focus on TextBox2.

<asp:TextBox ID="TextBox2" runat="server" onblur="ValidateTextNotNull()"></asp:TextBox>



function ValidateTextNotNull()
     {
         var Firstname = document.getElementById('TextBox2').value;
         var textLength = Firstname.length;
         if (textLength == 0) {
             alert("show your message");
             document.getElementById('TextBox2').focus();
         }

     }

解决方案

replace document.getElementById('TextBox2') with document.getElementById('<%=TextBox2.ClientID%>')

Please read the Documentation[^]

Quote:

When a Web server control is rendered as an HTML element, the id attribute of the HTML element is set to the value of the ClientID property. The ClientID value is often used to access the HTML element in client script by using the document.getElementById method.


这篇关于使用javascript在asp.net的Textfield上设置Focous的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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