天冬氨酸。网络javascript警报无法正常工作 [英] Asp. Net javascript Alert not working

查看:60
本文介绍了天冬氨酸。网络javascript警报无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以即时尝试使用JavaScript验证我的ASP.net论坛,但我的警报框无效。我可以不使用Alert作为其使用ASP吗?任何帮助,谢谢谢谢:)



<前lang =Javascript>< pre>函数userValid(){

var email = document .getElementById(' <%= txt_eadd.ClientID%>'

if (电子邮件。 value == ){
alert( 请输入有效的电子邮件地址。);
email.focus();
return false ;
}
if (email.value.indexOf( @ 0 )< 0 ){
alert( 请输入有效的电子邮件地址。);
email.focus();
return false ;
}
if (email.value.indexOf( 0 )< 0 ){
alert( 请输入有效的电子邮件地址。);
email.focus();
return false ;
}
返回 true ;
}

解决方案

警报始终有效;你根本就不会打电话给它,因为你在if中的条件永远不会是真的,或者很可能,你只是在第一个条件下得到一个例外。如果您不处理异常,代码将以静默方式终止。



例如,考虑如果您的元素 email 找不到。在这种情况下, document.getElementById 返回null:

Document.getElementById - Web API | MDN [ ^ ]。



尝试通过取取消引用空对象引用会引发异常。您可以了解使用JavaScript调试器时究竟会发生什么。



-SA


Hi, so im trying to validate my ASP.net forum using JavaScript however my alert boxes arent working. Can i not use Alert as its using ASP ? Any help in appreciated thanks :)

<pre>function userValid() {
   
    var email = document.getElementById('<%= txt_eadd.ClientID %>')

   if (email.value == "") {
        alert("Please enter a valid e-mail address.");
        email.focus();
        return false;
    }
    if (email.value.indexOf("@", 0) < 0) {
        alert("Please enter a valid e-mail address.");
        email.focus();
        return false;
    }
    if (email.value.indexOf(".", 0) < 0) {
        alert("Please enter a valid e-mail address.");
        email.focus();
        return false;
    }
    return true;
}

解决方案

Alert always works; you simply never call it, because your conditions in "if" never true, or, very likely, you simply get an exception on the first condition. If you don't handle exceptions, the code terminates silently.

For example, consider what happens if your element email is not found. In this case, document.getElementById returns null:
Document.getElementById — Web APIs | MDN[^].

An attempt to dereference null object reference by taking value throws an exception. You can find out what exactly happens if you use the JavaScript debugger.

—SA


这篇关于天冬氨酸。网络javascript警报无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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