在Javascript上出现错误 [英] getting error on Javascript

查看:40
本文介绍了在Javascript上出现错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用JavaScript验证用户控件内部的文本框

请找到以下代码.

 < script          类型  ="  > 
    调试器
    函数checkEmpty(){
         var  eleName = GetClientId('  txtName' );

        如果(document.getElementByID('  eleName'). = ' ')
   {
       alert(' 输入名称');
       返回 ;
    }
}

函数GetClientId(strid)
{


      var  count = document.forms [ 0 ].长度 ;
      var  i =  0  var  eleName;
      for (我=  0 ;我>< 计数; i ++)
     {
       eleName = document.forms [ 0 ].元素[i] .id;
       pos = eleName.indexOf(strid);
       如果(pos >  =  0 )  break ;
     }
    返回 eleName;
 } 

解决方案

难以计算eleName(顺便说一句,为什么?)之后,您忽略了它在函数checkEmpty()中的值,因为不使用它的值,所以在if (document.getElementByID(''eleName'').value = '''')中的引号中使用了变量名.设为if (document.getElementByID(''eleName'').value == '''');然后您将得到您可能想要做的事情. (还用"=="替换了"=",归功于phil.o.)

但是主要的问题是这个小代码的一般设计.最好重新考虑一下.



好吧,这里有许多较小的问题.为什么返回假checkEmpty()?您永远不会返回true,并且可能永远不会使用返回值. GetClientId的目的尚不清楚,因为,如果在从此函数返回之前不了解客户端id,那么为什么您认为该函数的用户知道strid?如果没有表格怎么办?在这种情况下,forms[0]将引发异常.如果多个id包含strid怎么办?什么,您认为只是第一次出现?为什么?

—SA


SA的评论是正确的.

还有一个错字:

 如果(文档 .getElementByID( eleName').value  =  ')


javascript中的比较运算符为==.

您应该写:

 如果(文档 .getElementByID(eleName).value  = =  ' ')


I want to validate a text box which is inside usercontrol using javascript

please find the below code.

<script    type="text/javascript">
    debugger
    function checkEmpty() {
        var eleName = GetClientId('txtName');

        if (document.getElementByID('eleName').value = '')
   {
       alert('Enter name');
       return false;
    }
}

function GetClientId(strid)
{


     var count=document.forms [ 0 ] . length ;
     var i = 0 ;
     var eleName;
     for (i = 0 ;  i < count ;  i++ )
     {
       eleName = document.forms [ 0 ] . elements [ i ] .id;
       pos=eleName.indexOf ( strid ) ;
       if(pos >= 0)  break;
     }
    return eleName;
 }

解决方案

After having hard time calculating that eleName (by the way, why?) you ignore its value in the function checkEmpty(), because you don''t use its value, you use the variable name in quotation marks in if (document.getElementByID(''eleName'').value = ''''). Make it if (document.getElementByID(''eleName'').value == ''''); and you will get what you probably meant to do. (Also replaced ''='' with ''=='', credit to phil.o.)

But the main problem is the general design of this small code. Better rethink it.

[EDIT]

Well, there is a number of smaller problems. Why returning false checkEmpty()? You never return true and probably never use return value. The purpose of GetClientId is not clear, because, if you don''t know the client id before getting return from this function, why do you thing that the user of this function knows strid? What if there are no forms? In this case, forms[0] will throw an exception. What if more than one id contains strid? What, do you think just first occurrence? Why?

—SA


SA''s remarks are true.

There is also a typo :

if (document.getElementByID('eleName').value = '')


Comparison operator in javascript is ==.

You should write :

if (document.getElementByID(eleName).value == '')


这篇关于在Javascript上出现错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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