I am checking if user exists in database if exists I am showing message as "existed user" and then I need to disable the signup butto开发者_开发问答n if not I need to enable it.
I am unable to enable and disable the sign Up button.
Can anyone help me in this issue?
Here is my code:
<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>
Unfortunately your problem is something as small as the difference between enabled
and disabled
.enabled = true;
Should be :
.disabled = false;
You can play with this:
$('#ButtonId').prop("disabled", true); ->> disabled
$('#ButtonId').prop("disabled", false); ->> Enabled
Try this...
document.getElementById('<%= button.ClientID %>').disabled = true;
OR
document.getElementById('<%= button.ClientID %>').disabled = false;
JavaScript:
function Enable() {
$("#btnSave").attr('disabled', false);
}
function Disable() {
$("#btnSave").attr('disabled', true);
}
ASPX Page:
<asp:Button runat="server" ID="btnSave" Text="Save" UseSubmitBehavior="false" OnClientClick="if(Page_ClientValidate('Validation')){javascript:Disable();}" ValidationGroup="Validation"/>
Code Behind:
ScriptManager.RegisterStartupScript(Me, Me.GetType(), "Disable", "javascript:Disable();", True)
u can just set visibility of your button to false visibility="false"
The .disabled does work, have you used break point to make sure your code was even being reached ? Your conditional if / else's may not be returning your expected values.
I do this:
Disable:
var myButton = document.getElementById('<%= this.myButton.ClientID %>');
$(myButton).attr('disabled', 'disabled');
Enable:
$(myButton).removeAttr('disabled');
To enable and disable the buttons using JavaScript, this is what should be done-
Example:
<asp:Button ID="Button1" runat="server" Text="Button" OnClientClick="a(); return false;"/>
<asp:Button ID="Button2" runat="server" Text="Button" OnClientClick="b(); return false;" />
and
<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>
NOTE: While other posts had similar code, they fail because of the reload on the page. So to avoid the reload a return false
should be added like OnClientClick="a(); return false;"
This JavaScript code should work.
document.getElementById("<%=btnSignUp.ClientID%>").disabled = true; //To disable the button
document.getElementById("<%=btnSignUp.ClientID%>").disabled = false;//To enable the button
Your code should look like
<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>
精彩评论