【问题标题】:Javascript: Set asp.net button visibility to falseJavascript:将 asp.net 按钮可见性设置为 false
【发布时间】:2015-01-20 05:42:08
【问题描述】:

我无法使用 javascript 隐藏我的按钮,我的代码是这样的。 (我使用图像作为按钮)

<img alt="" src="images/search-icon.png" width="16" height="16" style="display: inline;height:16px;cursor:pointer; width: 16px;" onclick="ViewLookUpPayment('<%=btnShowCorpCode.ClientID %>');" />

这是我的 javascript 中的代码。

function ViewLookUpPayment(bShow)
{
 var button1 = document.getElementById(bShow);
    button1.style.visibility = "visible";
}

每次点击图片都会弹出这个运行时错误

我不明白为什么会出现运行时错误,谁能帮我解决这个问题?

【问题讨论】:

  • 你的函数应该有名字吗?
  • @sh1rts 是的,有人刚刚编辑了我的问题。
  • 在按钮上将 ID 模式设置为静态。将 ID 设置为已知值。对 javascript 调用进行硬编码。在浏览器中打开并调试。验证您是否确实可以获取该元素,然后放松,直到找到问题为止。

标签: javascript asp.net button


【解决方案1】:

你是否为图片标签设置了 'id' 属性?

可能是“document.getElementById(bShow)”返回“undefined”。尝试检查“var button1”是什么。(例如:console.log(button1))

【讨论】:

    【解决方案2】:

    试试这个

    <img alt="" src="images/search-icon.png" 
    width="16" height="16" style="display: 
    inline;height:16px;cursor:pointer; width: 16px;" 
    onclick="ViewLookUpPayment('<%=btnShowCorpCode.ClientID %>');" />
    
    
    function ViewLookUpPayment(obj)
    {
    
        document.getElementById(obj).style.display = "block";
    }
    

    【讨论】:

    • bShow 是一个参数,不是硬编码值。这不能解决问题
    • @Ganesh_Devlekar 默认情况下,在加载页面时,btnShowCorpCode 的可见性设置为 false,这就是为什么即使我尝试了您的代码,我仍然会收到运行时错误。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-09-02
    • 2011-11-21
    • 1970-01-01
    • 1970-01-01
    • 2016-03-17
    • 2012-08-13
    • 2019-12-09
    相关资源
    最近更新 更多