【问题标题】:Asp.Net TextBox loses focus after typing a character for the first time第一次输入字符后,Asp.Net TextBox 失去焦点
【发布时间】:2021-10-27 01:40:36
【问题描述】:

我有一个包含文本框的 Asp.Net 网络表单 (aspx):

<div id="field">
   <asp:TextBox Id="Name" runat="server" MaxLength="50"/>
</div>    

我在 TextBox 中输入字符后进行 jQuery ajax 调用,它返回数据库中的名称列表。

$("#field input[id*='Name']").on("input", function(e) {
    $.ajax({
        url: my_WebAPI_url,
        type: 'Get',
        dataType: 'json',
        contentType: "application/json; charset=utf-8",
        async: true,
        success: function (result) {
                 console.log(result);
        }
    });
 }

第一次单击文本框或键入字符后,文本框失去焦点。我必须再次单击 TextBox,然后它可以正常工作而不会失去焦点。

如何让TextBox第一次点击后不失去焦点?

【问题讨论】:

  • 听起来你的页面上有其他东西导致了这种情况。不看其余代码就无法判断。

标签: javascript jquery asp.net ajax webforms


【解决方案1】:

你可以去那里参考一下

https://www.aspsnippets.com/Articles/Populate-jQuery-AutoComplete-TextBox-from-Database-using-Web-Service-in-ASPNet.aspx

How do you automatically set the focus to a textbox when a web page loads?

否则你也在你的页面加载代码文件

txtname.Focus();

    or

// inside page_load 
Page.Form.DefaultFocus = Yourpage.FindControl("txtname").ClientID;

   

【讨论】:

    猜你喜欢
    • 2019-12-19
    • 2021-12-14
    • 2022-01-16
    • 1970-01-01
    • 2019-05-15
    • 1970-01-01
    • 2020-06-03
    • 2023-03-20
    相关资源
    最近更新 更多