![](/img/trans.png)
[英]Form validation: un-hide submit button when a radio value is selected and text message is entered
[英]Show the Submit button, if and only if text (a character) is entered in a textbox else hide
我對編程領域比較陌生,這是一個初學者的問題。 在嘗試從頁面上的用戶那里獲取一些信息時,aspx頁面上有一個文本框和一個提交按鈕。
現在,我只想在用戶在文本框中輸入文本時顯示“提交”按鈕,否則就將其隱藏。
我認為onKeyDown或onKeyEvent應該可以幫助我完成此操作,但是它不能正常工作。 該按鈕僅在輸入2個字符后顯示,並且不會隱藏,除非刪除框內的最后一個字符后再次按退格鍵。 我不想使用<input
標記,並且不能使用<asp:Button
和Textbox標記。
碼:
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:TextBox ID="TextBox" onKeyDown = "showAndHide()" runat="server" Height="95px" Width="340px"
CssClass="SuggestionTxtBox" TextMode="MultiLine"
ToolTip="Enter your Suggestion here...." BorderStyle="Ridge"
ontextchanged="TextBox_TextChanged"></asp:TextBox>
<br />
<asp:Button ID="Button" runat="server" Text="Submit"
CssClass="SuggestionSubmitButton" onclick="Button_Click"
onclientclick=" " BackColor="#F5F5F5" Font-Size="13px"
ForeColor="#030756" Font-Bold="true" BorderStyle="None" Visible="False" />
<br />
<asp:Label ID="Label2" runat="server" Text="" CssClass="SuggestionFootNote" ForeColor="#030756" Font-Italic="True" Font-Size="12px"></asp:Label>
<p>
</p>
<input type="text" id="input1" onkeydown="showAndHide();" />
<input type="submit" id="submit" style="display: none;" />
<script type="text/javascript">
inputOne = document.getElementById("input1");
inputSubmit = document.getElementById("submit");
function showAndHide() {
if (inputOne.value == "") {
inputSubmit.style.display = "none";
}
else {
inputSubmit.style.display = "block";
}
}
</script>
</ContentTemplate>
</asp:UpdatePanel>'
請使用OnKeyUp
而不是onkeydown
如果是ASP.Net按鈕和文本框,則使用
inputOne = document.getElementById("<%= TextBox1.ClientID %>");
inputSubmit = document.getElementById("<%= Button1.ClientID %>");
在頁面加載中
If(!IsPostBack)
{
Button1.Attributes.Add("OnKeyUp","showAndHide();");
Button1.Style.Add("display","none");
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.