【问题标题】:JQUERY AJAX LOAD-TEXTBOX FOCUSJQUERY AJAX 加载文本框焦点
【发布时间】:2017-09-20 13:59:37
【问题描述】:

我正在尝试获取 div 中的文本以显示 div 旁边的文本框何时聚焦。我没有看到那个文字

下面是jquery脚本

<asp:Content ID="Content1" ContentPlaceHolderID="MainContent" runat="server">
    <script src="../Scripts/jquery-1.11.2.js"></script>
    <script type="text/javascript">
        $(document).ready(function () {
            //$jquery selector
            //find textbox and place it in a variable
            var txtbox = $('input[type="text"]');

            //when textbox recives focus
            txtbox.focus(function () {
                $('#newroleHelpDiv').load('help.html');
            });

            //when textbox loses focus
            txtbox.blur(function () {
                $('#newroleHelpDiv').html('');
            });
        });
    </script>

下面是 ASP 代码

    <fieldset style="width:350px">
            <legend> New Role</legend>
             <table>
                 <tr>
                     <td>Insert New Role:</td>
                     <td> <input type="text" id="newrole" /> </td>
                     <td> <div id="newroleHelpDiv" runat="server"></div></td>
                     </tr>
                 </table>
        </fieldset>
</asp:Content>

以下是帮助文本来自的 help.html 文件

<div id="newroleHelpDiv">
    You may add an employee to this role
</div>

【问题讨论】:

    标签: javascript jquery html asp.net ajax


    【解决方案1】:

    id 应该是唯一的,所以如果你有 id="newroleHelpDiv" 主 html,你应该为你的 help.html 使用另一个 id

    不确定是否可以加载 html 文件,但如果只加载一次,则可以像这样显示和隐藏:

    $(document).ready(function() {
      $('#newroleHelpDiv').load('help.html');
      $('#newroleHelpDiv').hide();
      //$jquery selector
      //find textbox and place it in a variable
      var txtbox = $('input[type="text"]');
    
      //when textbox recives focus
      txtbox.focus(function() {
        $('#newroleHelpDiv').show();
      });
    
      //when textbox loses focus
      txtbox.blur(function() {
        $('#newroleHelpDiv').hide();
      });
    });
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.2.3/jquery.min.js"></script>
    <fieldset style="width:350px">
      <legend> New Role</legend>
      <table>
        <tr>
          <td>Insert New Role:</td>
          <td>
            <input type="text" id="newrole" /> </td>
          <td>
            <div id="newroleHelpDiv" runat="server">You may add an employee to this role</div>
          </td>
        </tr>
      </table>
    </fieldset>

    【讨论】:

    • 试过了,但它不起作用。会不会是因为我试图在 .aspx 页面上加载 help.html 而没有显示?
    • @A.Ban 你至少可以加载 html 吗?
    • 我可以在浏览器中查看是的
    • @A.Ban 您是否更改了 help.html 中的id="newroleHelpDiv"?因为 id 必须是唯一的
    • @A.Ban 或者可能是路径?会不会像/help.htmlxxx/help.html
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-10-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-14
    相关资源
    最近更新 更多