Fire event on enter key press for a textbox

后端 未结 10 1442
[愿得一人]
[愿得一人] 2020-12-01 02:05

I have the following asp.net textbox control.


After the user writes something in this

相关标签:
10条回答
  • 2020-12-01 02:50

    my jQuery powered solution is below :)

    Text Element:

    <asp:TextBox ID="txtTextBox" ClientIDMode="Static" onkeypress="return EnterEvent(event);" runat="server"></asp:TextBox>
    <asp:Button ID="btnSubmitButton" ClientIDMode="Static" OnClick="btnSubmitButton_Click" runat="server" Text="Submit Form" />
    

    Javascript behind:

    <script type="text/javascript" language="javascript">
        function fnCheckValue() {
            var myVal = $("#txtTextBox").val();
            if (myVal == "") {
                alert("Blank message");
                return false;
            }
            else {
                return true;
            }
        }
    
        function EnterEvent(e) {
            if (e.keyCode == 13) {
                if (fnCheckValue()) {
                    $("#btnSubmitButton").trigger("click");
                } else {
                    return false;
                }
            }
        }
    
        $("#btnSubmitButton").click(function () {
            return fnCheckValue();
        });
    </script>
    
    0 讨论(0)
  • 2020-12-01 02:53

    ASPX:

    <asp:TextBox ID="TextBox1" clientidmode="Static" runat="server" onkeypress="return EnterEvent(event)"></asp:TextBox>    
    <asp:Button ID="Button1" runat="server" style="display:none" Text="Button" />
    

    JS:

    function EnterEvent(e) {
            if (e.keyCode == 13) {
                __doPostBack('<%=Button1.UniqueID%>', "");
            }
        }
    

    CS:

    protected void Button1_Click1(object sender, EventArgs e)
        {
    
        }
    
    0 讨论(0)
  • 2020-12-01 02:55
    <asp:Panel ID="Panel2" runat="server" DefaultButton="bttxt">
        <telerik:RadNumericTextBox ID="txt" runat="server">
        </telerik:RadNumericTextBox>
        <asp:LinkButton ID="bttxt" runat="server" Style="display: none;" OnClick="bttxt_Click" />
    </asp:Panel>
    

     

    protected void txt_TextChanged(object sender, EventArgs e)
    {
        //enter code here
    }
    
    0 讨论(0)
  • 2020-12-01 02:58
    <input type="text" id="txtCode" name="name" class="text_cs">
    

    and js:

    <script type="text/javascript">
    $('.text_cs').on('change', function () {
    var pid = $(this).val();
    console.log("Value text: " + pid);
    });
    </script>
    
    0 讨论(0)
提交回复
热议问题