使用 javascript 和 asp.net 启用和禁用按钮
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7514716/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Enable and disable button using javascript and asp.net
提问by coder
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 button 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>
回答by cillierscharl
Unfortunately your problem is something as small as the difference between enabled
and disabled
不幸的是,您的问题与enabled
和之间的差异一样小disabled
.enabled = true;
.enabled = true;
Should be :
应该 :
.disabled = false;
.disabled = false;
回答by Amr Elgarhy
You can play with this:
你可以玩这个:
$('#ButtonId').prop("disabled", true); ->> disabled
$('#ButtonId').prop("disabled", false); ->> Enabled
回答by sharmila
Try this...
尝试这个...
document.getElementById('<%= button.ClientID %>').disabled = true;
OR
或者
document.getElementById('<%= button.ClientID %>').disabled = false;
回答by Raj
JavaScript:
JavaScript:
function Enable() {
$("#btnSave").attr('disabled', false);
}
function Disable() {
$("#btnSave").attr('disabled', true);
}
ASPX Page:
ASPX 页面:
<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)
回答by Prakash Kumar Jha
u can just set visibility of your button to false visibility="false"
您可以将按钮的可见性设置为 false 可见性 =“false”
回答by EaziLuizi
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.
.disabled 确实有效,你有没有使用断点来确保你的代码甚至被到达?您的条件 if / else 可能不会返回您的预期值。
回答by Dan Ng
I do this:
我这样做:
Disable:
禁用:
var myButton = document.getElementById('<%= this.myButton.ClientID %>');
$(myButton).attr('disabled', 'disabled');
Enable:
使能够:
$(myButton).removeAttr('disabled');
回答by Joseph Mathew
This JavaScript code should work.
这个 JavaScript 代码应该可以工作。
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>
回答by Cdeez
To enable and disable the buttons using JavaScript, this is what should be done-
要使用 JavaScript 启用和禁用按钮,应该这样做-
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;"
注意:虽然其他帖子有类似的代码,但由于页面上的重新加载而失败。所以为了避免重新加载 areturn false
应该添加像OnClientClick="a(); return false;"