2011-09-22 76 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>