使用JavaScript启用/禁用基于空文本框的按钮

时间:2014-06-21 21:14:45

标签: javascript

使用一个字段验证按钮可以使用此代码。

<script language="javascript" type="text/javascript">
function SetButtonStatus(sender, target)
{
if ( sender.value.length >= 12 )
document.getElementById(target).disabled = false;
else
document.getElementById(target).disabled = true;
}
script>

<asp:TextBox ID="txtText" runat="server" onkeyup="SetButtonStatus(this, 'btnButton')">asp:TextBox>
<asp:Button ID="btnButton" runat="server" Text="Button" Enabled="false" />


<asp:TextBox ID="SECONDtxtText" runat="server" />

但我需要检查两个字段。我试过if else否则,但没有工作。 如何添加额外的字段来检查谢谢。

4 个答案:

答案 0 :(得分:0)

试试这个

<script language="javascript" type="text/javascript">
function SetButtonStatus()
{
var fv=document.getElementById('<%=txtText.ClientID%>').value;
var sv=document.getElementById('<%=SECONDtxtText.ClientID%>').value;
//Change these conditions as your requirement
if ( fv.length >= 12 && sv.length>=12)
document.getElementById('<%=btnButton.ClientID%>').disabled = false;
else
document.getElementById('<%=btnButton.ClientID%>').disabled = true;
}
script>

<asp:TextBox ID="txtText" runat="server" onkeyup="SetButtonStatus()">asp:TextBox>
<asp:Button ID="btnButton" runat="server" Text="Button" Enabled="false" />


<asp:TextBox ID="SECONDtxtText" runat="server" onkeyup="SetButtonStatus()" />

答案 1 :(得分:0)

建议:跳过onkeyup="SetButtonStatus(this, 'btnButton')">中的聪明论据,而不只是onkeyup="SetButtonStatus()">

function SetButtonStatus(){
    document.getElementById('btnButton').disabled =
        document.getElementById('txtText').value.length < 12 ||
        document.getElementById('SECONDtxtText').value.length < 12;
}

答案 2 :(得分:0)

首先你的html有错误

<body>
    <form id="form1" runat="server">
    <script>
        function SetButtonStatus(sender, target)
        {
            if ( sender.value.length >= 12 )
                document.getElementById(target).disabled = false;
            else
                document.getElementById(target).disabled = true;
        }
        </script>
        <asp:TextBox ID="txtText" runat="server" onkeyup="SetButtonStatus(this, 'btnButton')"></asp:TextBox>
        <asp:Button ID="btnButton" runat="server" Text="Button" Enabled="false" />
        <asp:TextBox ID="SECONDtxtText" runat="server"></asp:TextBox>
    </form>
</body>

答案 3 :(得分:0)

<html>
<head>
<script type="text/javascript">
function validateInput() {
var txt=document.getElementsByTagName("textarea");
pass = true;
for (var i=0; i<txt.length;i++) {
if (txt[i].value =="") {
pass = false;
} 
}
document.getElementById("myButton").disabled=!pass?true:false;
document.getElementById("myButton").value=!pass?(passICount+passTCount) + 
"Submit":"Submit";
}
</script>
</head>
<body onload="validateInput()" onmousedown="validateInput()" 
onkeyup="validateInput()">
<form onsubmit="">
<textarea name="myInput"></textarea>
<input type="button" id="myButton" value="Submit" />
</form>
</body>
</html>