[英]Enable and disable button using javascript and 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>
不幸的是,您的問題與enabled
和disabled
之間的差異一樣小
.enabled = true;
應該 :
.disabled = false;
你可以玩這個:
$('#ButtonId').prop("disabled", true); ->> disabled
$('#ButtonId').prop("disabled", false); ->> Enabled
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)
試試這個...
document.getElementById('<%= button.ClientID %>').disabled = true;
要么
document.getElementById('<%= button.ClientID %>').disabled = false;
你可以將你的按鈕的可見性設置為false visibility =“false”
.disabled確實有效,您是否使用了斷點來確保您的代碼被觸及? 您的條件if / else可能沒有返回您的預期值。
要使用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;"
我這樣做:
禁用:
var myButton = document.getElementById('<%= this.myButton.ClientID %>');
$(myButton).attr('disabled', 'disabled');
啟用:
$(myButton).removeAttr('disabled');
這個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>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.