[英]Blocking button when input textbox is empty asp.Net webforms
我的頁面上有一個按鈕和文本控件。
<asp:TextBox ID="MyText" Width="100%" ClientIDMode="Static" runat="server"></asp:TextBox>
<asp:Button ID="MyButton" OnClientClick="isNotEmpty()" CssClass="btn btn-primary" OnClick="Send_Click" ClientIDMode="Static" CausesValidation="false" runat="server" />
我試圖確保如果頁面上的文本框為空(或具有空格),它將不會觸發OnClick。 我的代碼不起作用,我也不知道為什么。
function isNotEmpty() {
if (<%= MyText.Text.Length %> > 0)
return true;
return false;
}
我也嘗試過:
function isNotEmpty(){
document.getElementbyId("MyText").value !== null || document.getElementbyId("MyText").value.match(/^\s*$/) === null
}
您需要在OnClientClick
使用return
<asp:Button ID="MyButton" OnClientClick="return isNotEmpty();" CssClass="btn btn-primary" OnClick="Send_Click" ClientIDMode="Static" CausesValidation="false" runat="server" />
您需要在客戶端上找到MyText
控件,不幸的是它的名稱不是"MyText"
。 它是由服務器分配的,並且可以根據服務器周圍的其他元素而有所不同。
function isNotEmpty(){
var textBox = document.getElementbyId("<%= MyText.ClientID %>");
return textBox.value !== null || textBox.value.match(/^\s*$/) === null;
}
您還需要將Agalo的答案合並到ASPX文件中。
OnClientClick="return isNotEmpty();"
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.