【问题标题】:focus on ASP dropdown using javascript/jquery on page load专注于在页面加载时使用 javascript/jquery 的 ASP 下拉菜单
【发布时间】:2017-11-10 11:22:49
【问题描述】:

我正在尝试专注于使用 javascript/jquery 加载页面的 ASP 下拉菜单。我可以使用DDL_Department.Focus() 方法通过后面的代码来做到这一点。但我正在尝试使用 javascript/jquery。

我尝试了三种方法,但都没有成功。请看代码。

<asp:Content ID="UserContent" runat="server" ContentPlaceHolderID="ContentPlaceHolder1">
<div class="form-group">
                        <label class="col-md-2 control-label">Department</label>
                        <div class="col-md-4">
                            <asp:DropDownList ID="DDL_Department" class="form-control input_text" runat="server">
                                <asp:ListItem Text="--Select Department--" Value="" />
                            </asp:DropDownList>
                        </div>
                    </div>
<script>
        //first approach
        //not successful
        $(function () {
            $('# <%= DDL_Department.ClientID %>').focus();
        });
        //////
        //second approach
        //not successful
        $(function () {
            $('#DDL_Department').focus();
        });
        //////
        //third approach
        //not successful
        $(function () {
            $("[id$=DDL_Department]").focus();
        });
        //////

    </script>
</asp:Content>

【问题讨论】:

  • 这里的“#”和“$('# <%= DDL_Department.ClientID %>').focus(); 不应该有空格。
  • 试试$("[id$=DDL_Department]").click(); 而不是.focus()
  • 没有一个解决方案有效。我不明白谁否定了我的问题。如果可以的话,最好提供解决方案或对负面标记的原因发表一些评论。

标签: javascript jquery asp.net dropdown


【解决方案1】:

你可以尝试改变:

$(function () {
        $('# <%= DDL_Department.ClientID %>').focus();
    });

到:

$(function () {
        $('#<%= DDL_Department.ClientID %>').focus();
    });

(删除磅字符后的空格)

【讨论】:

    【解决方案2】:

    你可以使用

    $('#DDL_Department').focus();
    

    没有别的。

    【讨论】:

    • 它工作正常。否则你必须添加一个处理函数。还要确保你的 jquery 文件正在加载。
    • 什么是处理函数?
    猜你喜欢
    • 2012-05-01
    • 2013-05-08
    • 2014-03-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-24
    • 2013-01-20
    • 1970-01-01
    • 2023-03-16
    相关资源
    最近更新 更多