我有一個用 vb 撰寫的 asp.net 網路表單,它有多個文本框和按鈕。如果一個文本框為空,我希望在按下回車鍵時單擊一個按鈕,而如果另一個文本框為空,我希望在按下回車鍵時單擊另一個按鈕。我知道我可以更改表單部分中的默認按鈕,但我不知道如何根據哪個文本框為空來更改默認按鈕?我假設我必須在 javascript 中執行此操作,我對此知之甚少,因此將不勝感激。
我可以做這樣的事情來更改默認按鈕嗎?
If txtMembranePressure.Text = "" Then
Dim sb As New System.Text.StringBuilder()
sb.Append("<form id='form1' runat='server'" "defaultbutton='btnMembranePressure'")
Else
Dim sb As New System.Text.StringBuilder()
sb.Append("<form id='form1' runat='server'" "defaultbutton='btnDiamondPressure'")
End If
我可以像這樣將默認按鈕直接添加到表單中嗎?
uj5u.com熱心網友回復:
擁有一鍵式例程不是更好嗎 - 所有按鈕都可以自由指向該一鍵式例程 - 但在該點擊例程中,您可以自由檢查給定文本框的值,然后運行所需的代碼. 與嘗試更改應該單擊的實際按鈕相比,這似乎要簡單得多。因此,讓所有按鈕運行相同的例程,但該例程可以簡單地檢查哪些文本框在其中具有值。
然后根據哪些文本框有(或沒有)值,您可以根據此資訊簡單地運行或呼叫您想要的代碼。
請記住,在大多數情況下,按 Enter 鍵將觸發該文本框之后標記中的控制元件的按鈕。編輯:更正:頁面上的 FIRST 按鈕將觸發。
但是,您可以通過在按鈕標記中設定 usesubmitBehaviour=False 來關閉此行為
<asp:TextBox ID="txtSearchOC" runat="server"></asp:TextBox>
<asp:Button ID="Button1" runat="server" Text="Button"
UseSubmitBehavior="False" />
實際上,如果您在表單上放置一個文本框,然后說一個有10行的gridview,并且gridviewe的每一行都有帶有click事件的按鈕?在上面按回車鍵實際上會觸發 gridview 的第一行按鈕點擊!!!
因此,通常通過正確放置按鈕,比如搜索文本框,以及單擊“搜索資料”之后的按鈕,那么事實上,如果他們按下回車鍵,則該文本框后面的按鈕無論如何都會觸發。(如前所述,標記上的第一個按鈕會觸發 - 不是任何按鈕,或者實際上不是文本框后面的那個)。
因此,在某些情況下,文本框的正確順序和后面的按鈕可以在這里得到很好的利用。但是,通常它會讓你感到驚訝。您放入一個文本框,然后一個表單后面有 10 個按鈕,其中一個將在您按下回車鍵時觸發 - 這通常很難防止這種情況發生。
所以,記住以上幾點。但是,假設您希望代碼基于文本框中的值(或缺少值)運行,那么我將擁有一個按鈕單擊 ALL 使用的例程,然后后面的代碼可以檢查文本框的值,并采用所需的操作程序并根據此資訊運行所需的代碼。
uj5u.com熱心網友回復:
有3個步驟要做。
- 您需要知道,何時更改文本框。為此,您可以使用
TexboxChanged Event. - 您需要知道,如果
Textbox is empty. - 您需要知道,如何更改
default button.
每個文本框都需要一個 TextboxChanged 事件。并且在每種情況下,您都應該檢查文本框是否為空。如果為空,則應將其設定為默認值。
在偽代碼中:
if Textbox.Text = "" then
set Textbox to default
有關文本框更改事件的更多資訊,請在搜索引擎(例如 duckduckgo.com)中搜索“文本框更改事件”:
https://meeraacademy.com/textbox-autopostback-and-textchanged-event-asp-net/
要更改默認按鈕,請考慮 Stackoverflow 上的以下答案: 如何在 ASP.Net 中為 TextBox 設定默認按鈕?
uj5u.com熱心網友回復:
我已經在下面為您提供了足夠的詳細資訊和示例代碼,以便您自己重新設計它,即使我不太了解您的要求。我同意上面的評論,這可能不是最好的方法。您最好檢查服務器端是否填充了文本框,然后在代碼中遵循不同的路徑。
JQuery 允許您按類名查找元素(.NET 中的 CssClass="",普通 HTML 元素中的 )
$(".ClassName") 使 JQuery 在頁面上找到具有該類名的所有元素。$("#Id") 使 JQuery 在頁面上找到具有該 Id 的所有元素。
data-whatYouWantToStore 是一種針對元素存盤資料的便捷方式,然后您可以使用 Javascript/JQuery 進行讀取。只需將其全部小寫以避免打擾它。
$(element).data("the name after the data-bit") 將為您獲取值。
您需要更改以使其運行的唯一位是在文本框中:
data-targetbuttonemptyclass="js-button-1" data-targetbuttonnotemptyclass="js-button-2"
如果屬性中的文本框為空,則設定您希望它在按下 enter 時單擊的按鈕的類,data-targetbuttonemptyclass以及如果屬性中存在文本則單擊的按鈕data-targetbuttonnotemptyclass。js-click-if-not-empty如果您希望文本框由“空/非空”JavaScript 處理,則必須在其上設定類。
<%@ Page Language="vb" AutoEventWireup="false" CodeBehind="Buttons.aspx.vb" Inherits="Scrap.Buttons" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<!--Add reference to Jquery CDN -->
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<!--Your Javascript -->
<script type="text/javascript">
$(document).ready(function () {
// find all the buttons we want to set this behaviour on
$(".js-click-if-not-empty").each(function () {
// add a keypress event hander to each button
$(this).on("keypress", function () {
// get the key that was pressed
var keycode = (event.keyCode ? event.keyCode : event.which);
// is it the ENTER key?
if (keycode === 13) {
// prevent anything else that was going to happen because enter was pressed.
event.preventDefault();
// is the textbox empty?
if ($(this).val() === "") {
// yes - get the css class of the button to click when the textbox is empty
var button = $("." $(this).data("targetbuttonemptyclass"))[0];
// just for debugging to show which button is about to be clicked
alert("going to click empty button: " button.id);
// click the button
button.click();
} else {
// no - get the css class of the button to click when the textbox is not empty
var button = $("." $(this).data("targetbuttonnotemptyclass"))[0];
// just for debugging to show which button is about to be clicked
alert("going to click not empty button: " button.id);
// click the button
button.click();
}
};
});
});
});
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:TextBox ID="tb_TextBox1" runat="server" CssClass="js-click-if-not-empty" data-targetbuttonemptyclass="js-button-1" data-targetbuttonnotemptyclass="js-button-2"></asp:TextBox>
<asp:TextBox ID="tb_TextBox2" runat="server" CssClass="js-click-if-not-empty" data-targetbuttonemptyclass="js-button-1" data-targetbuttonnotemptyclass="js-button-2"></asp:TextBox>
<asp:TextBox ID="tb_TextBox3" runat="server" CssClass="js-click-if-not-empty" data-targetbuttonemptyclass="js-button-1" data-targetbuttonnotemptyclass="js-button-2"></asp:TextBox>
<asp:Button ID="btn_ClickIfEmpty" runat="server" CssClass="js-button-1" Text="Click If Empty" />
<asp:Button ID="btn_ClickIfNotEmpty" runat="server" CssClass="js-button-2" Text="Click If Not Empty" />
</div>
</form>
</body>
</html>
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/495067.html
