javascript textbox1为null [英] javascript textbox1 is null

查看:102
本文介绍了javascript textbox1为null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我有一个名称为 txt1 的文本框.

在javascript中,我将 txt1 设为必填项,但现在我添加了一个下拉列表.

因此,现在 txt1 仅在选择下拉列表的特定值时才是必需的.

这意味着,当我从下拉列表中选择 name 时,则必须 txt1 ,而当我从下拉列表中选择 class 时, txt1 >应该设置为可见关闭.

现在,当我在下拉列表中选择 class 时,使 txt1 可见为false,但这会抛出 javascript错误,表明 txt1 在下一行中为null.

但是当我从下拉列表中选择 name 时,就不会出现错误,因为它是可见的.

Hi
I have a textbox with name txt1.

In javascript, I am making txt1 mandatory, but now I have a added dropdownlist.

So, now txt1 is mandatory only when, the specific value of dropdown is selected.

That means, when I select name from dropdown, then txt1 is mandatory and when I select class from dropdown the txt1 should set to visible off.

Now I make txt1 visible to false, when i select class in dropdown, but it is throwing javascript error that txt1 is null in following line.

But when I select name from dropdown then no error comes because it is visible.

if(document.getElementById('<%=Txt1.ClientID%>').value=="")

推荐答案

如果使它在ASP.NET中不可见代码,那么它就不会被渲染.因此,它确实为空,不存在.我不确定为什么其他回答者感到困惑,很明显,您是从发布的代码中使用ASP.NET的,这就是它的工作方式.

总的来说,您应该始终编写代码来检查对象是否存在,然后再访问它.您应该转而使用jquery,它将解决您的问题(如果找不到该对象,它只会不返回任何内容),并在可能的情况下移至ASP.NET MVC.
If you make it not visible in your ASP.NET code, then it won''t be rendered. As such, it is indeed null, it is not there. I''m not sure why the other answerers are confused, it''s obvious you''re using ASP.NET from the code you posted, and that this is how it works.

Overall, you should always write code that checks if an object exists before accessing it. You should move towards using jquery, which would solve your issue ( it just doesn''t return anything if the object is not found ), and move to ASP.NET MVC if you can, in the future.


这篇关于javascript textbox1为null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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