使用javascript和asp.net启用和禁用按钮
我正在检查数据库中是否存在用户,如果存在,我将消息显示为“已存在用户”,然后我需要禁用注册按钮,如果不存在,我需要启用它 我无法启用和禁用“注册”按钮 在这个问题上有人能帮我吗 这是我的密码:使用javascript和asp.net启用和禁用按钮,javascript,asp.net,Javascript,Asp.net,我正在检查数据库中是否存在用户,如果存在,我将消息显示为“已存在用户”,然后我需要禁用注册按钮,如果不存在,我需要启用它 我无法启用和禁用“注册”按钮 在这个问题上有人能帮我吗 这是我的密码: <script type="text/javascript"> $(function () { $("#<% =btnavailable.ClientID %>").click(function () { if ($("#&l
<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>
$(函数(){
$(“#”)单击(函数(){
if($(“#”)val()=“”){
$(“#”).removeClass().addClass('notavailablecss').text('Required field不能为空').fadeIn(“慢”);
}否则{
$(“#”).removeClass().addClass('messagebox').text('Checking…').fadeIn(“slow”);
$.post(“LoginHandler.ashx”,{uname:$(“#”).val()},函数(结果){
如果(结果=“1”){
$(“#”)addClass('notavailablecss').fadeTo(900,1);
document.getElementById(#')。enabled=false;
}
否则如果(结果=“0”){
$(“#”)addClass('availablecss').fadeTo(900,1);
document.getElementById('#').enabled=true;
}
否则{
$(“#”)addClass('notavailablecss').fadeTo(900,1);
}
});
}
});
$(“#”)ajaxError(函数(事件、请求、设置、错误){
警报(“请求页面出错”+settings.url+“错误:+Error”);
});
});
不幸的是,您的问题与启用的和禁用的之间的差异一样小
.enabled=true代码>
应该是:
.disabled=false代码>您可以玩这个:
$('#ButtonId').prop("disabled", true); ->> disabled
$('#ButtonId').prop("disabled", false); ->> Enabled
试试这个
document.getElementById('<%= button.ClientID %>').disabled = true;
document.getElementById(“”).disabled=true;
或
document.getElementById(“”).disabled=false;
u只需将按钮的可见性设置为false visibility=“false”要使用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;" />
及
函数a(){
警报(“1”);
document.getElementById(“”).disabled=true;
document.getElementById(“”).disabled=false;
}
函数b(){
警报(“2”);
document.getElementById(“”).disabled=true;
document.getElementById(“”).disabled=false;
}
注意:虽然其他帖子也有类似的代码,但由于页面重新加载,它们失败了。因此,为了避免重新加载,应该像添加OnClientClick=“a();return false;”“
JavaScript:
function Enable() {
$("#btnSave").attr('disabled', false);
}
function Disable() {
$("#btnSave").attr('disabled', true);
}
<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)
ASPX页面:
function Enable() {
$("#btnSave").attr('disabled', false);
}
function Disable() {
$("#btnSave").attr('disabled', true);
}
<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)
.disabled确实有效,您是否使用断点来确保代码被访问?您的条件if/else可能没有返回您的期望值。我这样做:
禁用:
var myButton = document.getElementById('<%= this.myButton.ClientID %>');
$(myButton).attr('disabled', 'disabled');
这个JavaScript代码应该可以工作
document.getElementById("<%=btnSignUp.ClientID%>").disabled = true; //To disable the button
document.getElementById("<%=btnSignUp.ClientID%>").disabled = false;//To enable the button
document.getElementById(“”).disabled=true//禁用按钮
document.getElementById(“”).disabled=false//要启用该按钮
您的代码应该如下所示
<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>
$(函数(){
$(“#”)单击(函数(){
if($(“#”)val()=“”){
$(“#”).removeClass().addClass('notavailablecss').text('Required field不能为空').fadeIn(“慢”);
}否则{
$(“#”).removeClass().addClass('messagebox').text('Checking…').fadeIn(“slow”);
$.post(“LoginHandler.ashx”,{uname:$(“#”).val()},函数(结果){
如果(结果=“1”){
$(“#”)addClass('notavailablecss').fadeTo(900,1);
document.getElementById(“”).disabled=true;
}
否则如果(结果=“0”){
$(“#”)addClass('availablecss').fadeTo(900,1);
document.getElementById(“”).disabled=false;
}
否则{
$(“#”)addClass('notavailablecss').fadeTo(900,1);
}
});
}
});
$(“#”)ajaxError(函数(事件、请求、设置、错误){
警报(“请求页面出错”+settings.url+“错误:+Error”);
});
});
是的,我已经这样做了,如上所示,但当用户可用时,我单击“注册”按钮,它将刷新页面。我看到您正在使用POST
,因此我假设您只是在按钮上的事件处理程序中缺少event.preventDefault()
。将提交单击处理程序添加到原始帖子中。我也非常喜欢jquery,我不认为OP正在使用它。对不起,习惯于一眼就看标签;)是的,我尝试了与上面代码中提到的相同的事情,但不起作用。我尝试将“var”设置为该值,然后将“disabled”设置为“disabled”,但什么都没有发生。当我使用上述代码时,一切都正常。我相信这与“var”变量没有被理解为按钮有关,上面的代码仍然是这样理解的?现在有人可以帮你吗?我的问题是无法禁用和启用注册按钮。如果用户名存在,我需要禁用它,如果没有,我需要启用它。是的,我也这样做了,但是当用户单击显示可用性时,如果用户存在于数据库中,则我需要启用注册按钮,这在在c#code buttoneName.visibility=true或false中,必须使用带有某些条件的if-else语句