我如何才能使禁用的控件恢复使用Javascript来启用 [英] How can i make a disabled control back to enable using Javascript

查看:65
本文介绍了我如何才能使禁用的控件恢复使用Javascript来启用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经完成了一个代码,可以在用户单击控件时禁用该控件.在我的表单上,我有一个TextBox和一个DropDown.当用户单击TextBox时,我将禁用DropDown,就像当单击DropDown时,我将禁用TextBox一样.

但是,当用户单击禁用的控件"时,我想启用该控件.意味着如果我单击已禁用的TextBox,我也想为下拉菜单启用它..

我的示例脚本如下

I have done a code to disable a control when user clicks on a control. On my form i am having a TextBox and a DropDown. When user clicks on a TextBox i will disable the DropDown like that when click on DropDown i will disable the TextBox which works fine.

But when the user clicks on Disabled control i would like to enable that control. Means if i click on TextBox which was disabled i would like to Enable it like that for dropdown too..

My sample Script is as follows

<script type="text/javascript">
function toggleDropDownList1()
{
var MyDiv = document.getElementById("DivForClick");
var MyDdl = document.getElementById("<%= DropDownList4.ClientID %>");
MyDiv.style.display = "none";

var d=document.getElementById("<%= DropDownList4.ClientID %>");
if(d.disabled)
{
    d.disabled=false;
    document.getElementById("<%= TextBox1.ClientID %>").disabled = true;
}
else
{
document.getElementById("<%= TextBox1.ClientID %>").disabled = true;
}
MyDiv.style.display = "block";
MyDiv.style.left = MyDdl.style.left;
MyDiv.style.top = MyDdl.style.top;
}

 function toggleDropDownList2()
{
var MyDiv = document.getElementById("Div1");
var MyDdl = document.getElementById("<%= TextBox1.ClientID %>");
MyDiv.style.display = "none";

var d=document.getElementById("<%= TextBox1.ClientID %>");
if(d.disabled)
{
    d.disabled=false;
    document.getElementById("<%= DropDownList4.ClientID %>").disabled = true;
}
else
{
document.getElementById("<%= DropDownList4.ClientID %>").disabled = true;
}
MyDiv.style.display = "block";
MyDiv.style.left = MyDdl.style.left;
MyDiv.style.top = MyDdl.style.top;
}
</script>

<div id="Div1" onclick="toggleDropDownList2();" style="z-index: 999; position: absolute;
            left: 71px; top: 17px; height: 20px; width: 40px; display: none;">
        </div>
        <asp:TextBox ID="TextBox1" runat="server" onclick="toggleDropDownList2();"></asp:TextBox>
        <br />
        <br />
        <div id="DivForClick" onclick="toggleDropDownList1();" style="z-index: 999; position: absolute;
            left: 13px; top: 61px; height: 20px; width: 40px; display: none;">
        </div>
        <asp:DropDownList ID="DropDownList4" runat="server" onclick="toggleDropDownList1();"

            Style="z-index: 2;">
            <asp:ListItem Text="One" Value="1"></asp:ListItem>
            <asp:ListItem Text="Two" Value="2"></asp:ListItem>
        </asp:DropDownList>




但是我无法获得所需的一个,任何一个都可以帮助我




But i am unable to get the one as per required can any one help me

推荐答案

尝试一下
<div id="divval" onclick="CheckStat();"  >
    <asp:TextBox ID="txtTest" runat="server" ></asp:TextBox>
   </div>





<script type="text/javascript" language="javascript">
        function CheckStat() {
            var txtId = document.getElementById('<%=txtTest.ClientID%>');
            alert(txtId.disabled);
            if (txtId.disabled == true) {
                txtId.disabled = false;
            }
            else {
                txtId.disabled = true;
            }
        }
    </script>


如果您在文本框附近单击外部,它将起作用.您可以调整用户界面


If you click outside, near textbox, it will work. You can adjust the UI


<div id="divval" onclick="CheckStat();"  >

   <asp:TextBox ID="txtTest" runat="server" ></asp:TextBox>
  </div>
  <div id="div1" onclick="CheckStat();">
  <asp:DropDownList ID="drp1" runat="server" >
  <asp:ListItem Text="one"></asp:ListItem>
  <asp:ListItem Text="two"></asp:ListItem>
  </asp:DropDownList>
  </div>


<script type="text/javascript" language="javascript">
        function CheckStat() {
            var txtId = document.getElementById('<%=txtTest.ClientID%>');
            var drpId = document.getElementById('<%=drp1.ClientID%>');
            alert(txtId.disabled);
            if (txtId.disabled == true) {

                txtId.disabled = false;
                drpId.disabled = "disable";
            }
            else {

                txtId.disabled = true;
                drpId.disabled = "";
            }
        }
    </script>


引用该线程
http://forums.asp.net/t/1288462.aspx/1 [ ^ ]
Refer this thread
http://forums.asp.net/t/1288462.aspx/1[^]


这篇关于我如何才能使禁用的控件恢复使用Javascript来启用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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