简体   繁体   English

使用Javascript使按钮不可见或可见

[英]Make a button invisible or visible using Javascript

I need to use an OnFocus event of a textbox in ASP.Net. 我需要在ASP.Net中使用文本框的OnFocus事件。

 <asp:TextBox ID="TBAccountNum" runat="server" CssClass="textbox" Height="12px" Width="100px" Font-Size="Small" AutoCompleteType="None"  Wrap="False" OnFocus="Validator()" OnTextChanged="TBAccountNum_OnLeave" AutoPostBack="true"></asp:TextBox>

The only way to do that is to use Javascript, which I know nothing about. 唯一的方法就是使用Javascript,对此我一无所知。 I found this little piece of code on SO that looks like it should work: 我在SO上发现了这段小代码,看起来应该可以工作:

<asp:Content runat="server" ID="FeaturedContent" ContentPlaceHolderID="FeaturedContent">

<script type="text/javascript">

    function Validator()
    {
        document.getElementsByName("btnValidateWork").style.visibility = "visible"; 
        document.getElementsByName("btnSubmitWork").style.visibility = "hidden"
    }

However, when I run it I get the following error: 但是,当我运行它时,出现以下错误:

0x800a138f - Microsoft JScript runtime error: Unable to set value of the property 'visibility': object is null or undefined 0x800a138f-Microsoft JScript运行时错误:无法设置属性“ visibility”的值:对象为null或未定义

Any ideas what I'm doing wrong (besides trying to write code I know nothing about... :oP )? 任何想法我在做什么错(除了尝试编写代码,我对...:oP一无所知)?

All relevant code has now been posted. 所有相关代码现已发布。 I'm using IE9 on an Intranet (mandated by the company, so that can't change) if that matters. 如果这很重要,我将在Intranet上使用IE9(该公司规定,因此无法更改)。

document.getElementsByName("btnValidateWork") returns an htmlcollection. document.getElementsByName(“ btnValidateWork”)返回htmlcollection。 You have to iterate it, and for each item (node) add the proper css. 您必须对其进行迭代,并为每个项目(节点)添加适当的CSS。

Check out this: Using document.getElementsByName() isn't working? 检查一下: 使用document.getElementsByName()不起作用吗?

EDIT: 编辑:

to use getElementsByName, your element(s) must have a name property. 要使用getElementsByName,您的元素必须具有name属性。 Example: 例:

<button name="btnValidateWork">my button?</button>
document.getElementsByName("btnValidateWork")[0].style.visibility = "hidden";

Fiddle: http://jsfiddle.net/chrisbenseler/h3dwwud6/ 小提琴: http : //jsfiddle.net/chrisbenseler/h3dwwud6/

I think that a better approach is to use getElementsByClassName and add some class to it, and then do something like document.getElementsByClassName("hidden") 我认为更好的方法是使用getElementsByClassName并向其中添加一些类,然后执行诸如document.getElementsByClassName(“ hidden”)之类的操作

As said before document.getElementsByName returns a collection (array) of HTML-Objects. 如前所述, document.getElementsByName返回HTML对象的集合(数组)。 So you need to address them like this: 因此,您需要像这样解决它们:

 function Validate()
    {
        // To access the first
        document.getElementsByName("btnValidateWork")[0].style.visibility = "visible"; 
        document.getElementsByName("btnSubmitWork")[0].style.visibility = "hidden"
        // To access all of them
       var i = 0,
           validWork = document.getElementsByName("btnValidateWork");
       for (i = 0; i < validWork.length; i++) {
           validWork[i].style.visibility = "visible";
           // ...
       }
    }

See a working example here . 在这里查看工作示例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM