2011-09-22 75 views
9

我正在检查用户是否存在数据库中,如果存在我显示消息为“已存在的用户”,然后我需要禁用注册按钮,如果不是我需要启用它。使用JavaScript和asp.net启用和禁用按钮

我无法启用和禁用注册按钮。

任何人都可以帮助我解决这个问题吗?

这里是我的代码:

<script type="text/javascript"> 
    $(function() { 
     $("#<% =btnavailable.ClientID %>").click(function() { 
      if ($("#<% =txtUserName.ClientID %>").val() == "") { 
       $("#<% =txtUserName.ClientID %>").removeClass().addClass('notavailablecss').text('Required field cannot be blank').fadeIn("slow"); 

      } else { 
       $("#<% =txtUserName.ClientID %>").removeClass().addClass('messagebox').text('Checking...').fadeIn("slow"); 
       $.post("LoginHandler.ashx", { uname: $("#<% =txtUserName.ClientID %>").val() }, function (result) { 
        if (result == "1") { 
         $("#<% =txtUserName.ClientID %>").addClass('notavailablecss').fadeTo(900, 1); 
         document.getElementById(#<% =btnSignUp.ClientID %>').enabled = false; 
        } 
        else if (result == "0") { 
         $("#<% =txtUserName.ClientID %>").addClass('availablecss').fadeTo(900, 1); 
         document.getElementById('#<% =btnSignUp.ClientID %>').enabled = true; 
        } 
        else { 
         $("#<% =txtUserName.ClientID %>").addClass('notavailablecss').fadeTo(900, 1); 
        } 
       }); 
      } 
     }); 

     $("#<% =btnavailable.ClientID %>").ajaxError(function (event, request, settings, error) { 
      alert("Error requesting page " + settings.url + " Error:" + error); 
     }); 
    }); 
</script> 
+0

而你的问题是什么?现在人怎么能帮你? – Max

+0

我的问题是无法禁用和启用注册按钮。如果用户名存在,我需要禁用它,如果没有,我需要启用它。 – coder

回答

10

不幸的是你的问题是什么小enableddisabled的区别

.enabled = true;

应该是:

.disabled = false;

+0

是的我已经完成了如上所示,但当用户可用,我点击注册按钮它刷新页面。 – coder

+0

我看到你正在使用'POST',所以我假设你只是在你的按钮的事件处理函数中缺少event.preventDefault()'。 – f0x

+0

将您的提交点击处理程序添加到您的原始文章。 – f0x

8

你可以玩这个:

$('#ButtonId').prop("disabled", true); ->> disabled 
$('#ButtonId').prop("disabled", false); ->> Enabled 
+0

尽管我也热爱jquery,但我不认为OP会使用它。 – f0x

+0

@ f0x通过OP发布的代码,jQuery就在眼前! –

+0

对不起,一眼看标签的习惯;) – f0x

4

尝试......

document.getElementById('<%= button.ClientID %>').disabled = true; 

OR

document.getElementById('<%= button.ClientID %>').disabled = false; 
+0

我已经尝试了与上面的代码中提到的相同的东西,但不工作。 – coder

1

ü可以,只要将按钮的能见度为false知名度= “假”

+0

是的,我也这样做过,但是当用户点击显示可用性时,如果用户存在于数据库中,那么我需要启用注册按钮,这不是 – coder

+0

,因为你必须在某些条件下使用if else语句并在C#代码buttoneName.visibility = true或false –

0

要启用和禁用使用JavaScript的按钮,这是应该做的 -

例子:

<asp:Button ID="Button1" runat="server" Text="Button" OnClientClick="a(); return false;"/> 
<asp:Button ID="Button2" runat="server" Text="Button" OnClientClick="b(); return false;" /> 

<script type="text/javascript"> 
    function a() { 
     alert('1'); 
     document.getElementById('<%=Button1.ClientID %>').disabled = true; 
     document.getElementById('<%=Button2.ClientID %>').disabled = false; 
     } 
    function b() { 
     alert('2'); 
     document.getElementById('<%=Button2.ClientID %>').disabled = true; 
     document.getElementById('<%=Button1.ClientID %>').disabled = false; 
     } 
</script> 

注:在其他职位也有类似的代码,他们会失败,因为在页面上重装的。因此,为了避免重装一个return false应该像OnClientClick="a(); return false;"

5

JavaScript的加入:

function Enable() { 
     $("#btnSave").attr('disabled', false);     
    } 
function Disable() { 
     $("#btnSave").attr('disabled', true); 
    } 

ASPX页面:

<asp:Button runat="server" ID="btnSave" Text="Save" UseSubmitBehavior="false" OnClientClick="if(Page_ClientValidate('Validation')){javascript:Disable();}" ValidationGroup="Validation"/> 

代码背后:

ScriptManager.RegisterStartupScript(Me, Me.GetType(), "Disable", "javascript:Disable();", True) 
1

.disabled确实有效,您是否使用了断点来确保您的代码已经达到?您的条件if/else可能不会返回您的期望值。

0

我这样做:

禁用:

var myButton = document.getElementById('<%= this.myButton.ClientID %>'); 
    $(myButton).attr('disabled', 'disabled'); 

启用:

$(myButton).removeAttr('disabled'); 
0

这段JavaScript代码应该工作。

document.getElementById("<%=btnSignUp.ClientID%>").disabled = true; //To disable the button 

document.getElementById("<%=btnSignUp.ClientID%>").disabled = false;//To enable the button 

你的代码看起来应该像

<script type="text/javascript"> 
    $(function() { 
     $("#<% =btnavailable.ClientID %>").click(function() { 
      if ($("#<% =txtUserName.ClientID %>").val() == "") { 
       $("#<% =txtUserName.ClientID %>").removeClass().addClass('notavailablecss').text('Required field cannot be blank').fadeIn("slow"); 

      } else { 
       $("#<% =txtUserName.ClientID %>").removeClass().addClass('messagebox').text('Checking...').fadeIn("slow"); 
       $.post("LoginHandler.ashx", { uname: $("#<% =txtUserName.ClientID %>").val() }, function (result) { 
        if (result == "1") { 
         $("#<% =txtUserName.ClientID %>").addClass('notavailablecss').fadeTo(900, 1); 
         document.getElementById("<%=btnSignUp.ClientID%>").disabled = true; 
        } 
        else if (result == "0") { 
         $("#<% =txtUserName.ClientID %>").addClass('availablecss').fadeTo(900, 1); 
         document.getElementById("<%=btnSignUp.ClientID%>").disabled = false; 
        } 
        else { 
         $("#<% =txtUserName.ClientID %>").addClass('notavailablecss').fadeTo(900, 1); 
        } 
       }); 
      } 
     }); 

     $("#<% =btnavailable.ClientID %>").ajaxError(function (event, request, settings, error) { 
      alert("Error requesting page " + settings.url + " Error:" + error); 
     }); 
    }); 
</script>