【问题标题】:Javascript unable to disable asp.NET linkButtonJavascript 无法禁用 asp.NET linkBut​​ton
【发布时间】:2015-07-18 20:29:40
【问题描述】:

我查看了网络和我的代码,但找不到问题所在。请帮忙。我的代码如下。我创建了一些链接按钮,它们调用回发到后端,触发 javascript 禁用/启用链接按钮。不幸的是,我无法这样做。甚至可以通过javascript完成吗?知道为什么吗?

请帮忙...谢谢

<ul class="nav nav-tabs">
      <li role="presentation" class="active" ID="tab1"><asp:LinkButton ID="tab1Link" runat="server"><strong>Basic</strong></asp:LinkButton></li>
      <li role="presentation" ID="tab2"><asp:LinkButton ID="tab2Link" runat="server"><strong>Details+</strong></asp:LinkButton></li>
      <li role="presentation" ID="tab3"><asp:LinkButton ID="tab3Link" runat="server"><strong>Location</strong></asp:LinkButton></li>
      <li role="presentation" ID="tab4"><asp:LinkButton ID="tab4Link" runat="server"><strong>Media</strong></asp:LinkButton></li>
      <li role="presentation" ID="tab5"><asp:LinkButton ID="tab5Link" runat="server" OnClientClick="javascript: tabSeq('tab5', 5)"><strong>Summary</strong></asp:LinkButton></li>
    </ul>




function tabSeq(activeTab, numberOfTabs) 
{
var tabOKVal = "";
for (var i = 1; i <= numberOfTabs; i++)
{
    document.getElementById('tab'+ i).className = "";
    document.getElementById('tab'+ i+"Div").style.display = 'none';

    tabOKVal = document.getElementById('tabOK'+ i).value;
    if(tabOKVal == "OK" && i != numberOfTabs)
    {
        document.getElementById('tab' + (i+1) + 'Link').disabled = "";
        document.getElementById('tab' + (i+1) + 'Link').style.color = '#3498DB';
    }
}
document.getElementById('tab5Link').disabled = "disabled";
document.getElementById(activeTab).style.color = '#555555';
document.getElementById(activeTab).className = "active";
document.getElementById(activeTab+"Div").style.display = '';
}

【问题讨论】:

    标签: javascript asp.net


    【解决方案1】:

    onload = function(){
       tabSeq('tab5', 5)
    }
    
    function tabSeq(activeTab, numberOfTabs) 
    {
    //other stuff goes here
    document.getElementById('tab5Link').disabled = true;
    document.getElementById(activeTab).style.color = '#555555';
    document.getElementById(activeTab).className = "active";
    document.getElementById(activeTab+"Div").style.display = '';
    }
    <ul class="nav nav-tabs">
          <li role="presentation" class="active" ID="tab1"><asp:LinkButton ID="tab1Link" runat="server"><strong>Basic</strong></asp:LinkButton></li>
          <li role="presentation" ID="tab2"><asp:LinkButton ID="tab2Link" runat="server"><strong>Details+</strong></asp:LinkButton></li>
          <li role="presentation" ID="tab3"><asp:LinkButton ID="tab3Link" runat="server"><strong>Location</strong></asp:LinkButton></li>
          <li role="presentation" ID="tab4"><asp:LinkButton ID="tab4Link" runat="server"><strong>Media</strong></asp:LinkButton></li>
          <li role="presentation" ID="tab5"><button ID="tab5Link" runat="server" OnClientClick="javascript: tabSeq('tab5', 5)"><strong>Summary</strong></button></li>
        </ul>

    【讨论】:

      【解决方案2】:

      LinkBut​​tons 是 ASP.NET 服务器控件,您在设计时看到的控件 ID 值无法访问它们。

      可以改写js代码如下:

      document.getElementById("<%= tab5Link.ClientID %>").disabled = "disabled";
      

      请注意,只有JS代码在同一页面上才能做到这一点。

      【讨论】:

      • 谢谢!我放弃了,最后通过服务器端做到了。但是你的解释是正确的。但在那种情况下,为什么我可以通过 javascript 获取 asp:Textbox?不是怀疑你,只是在做事的方式上有点不一致:/
      • 我没有得到你的问题。你能告诉我你通过JS访问TextBox控件的地方吗?只需复制粘贴代码。 :)
      • 这不是一个真正的问题。这更像是一种观察。您说“ASP.NET 服务器控件,您在设计时看到的控件 ID 值无法访问它们”,但实际上,您可以使用控件 ID 访问其他 ASP 结构,例如。文本框。我的观点是,不要像访问 LinkBut​​tons 那样访问 Textboxes 似乎不一致。 :)
      • 所有服务器控件(按钮、文本框等)都不能通过它们在 JavaScript 代码中的 ID 值来访问。我们应该获取 clientID 来访问它们。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-08-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多