Onfocus 文本框用户名放入文本框名字值和文本框姓氏值拆分为

Onfocus textbox username put in textbox firstname value and textbox lastname value splitted with

一开始我有输入字段,它与 jquery 代码一起工作得很好,现在我将输入字段更改为 asp.net 文本框,因为我扩展了它以便将数据插入数据库。所以现在我需要弄清楚它是如何与 asp:textboxes.

一起工作的

我有 3 个 asp.net 文本框用于名字、姓氏和用户名

<asp:TextBox ID="TextBox_firstname" runat="server" name="firstname></asp:TextBox>

<asp:TextBox ID="TextBox_lastname" runat="server" name="lastname"></asp:TextBox>

<asp:TextBox ID="TextBox_username" runat="server" name="username"></asp:TextBox>

我不认为是因为我有错误的 jquery 文件 我使用的 jquery 文件是这样的:

<script src="Scripts/jquery-1.9.1.js"></script>

$("#<%=TextBox_username.ClientID%>").focus(function () {
    var firstname = $("#<%=TextBox_firstname.ClientID%>").val();
    var lastname = $("#<%=TextBox_lastname.ClientID%>").val();
    if (firstname && lastname && !this.value) {
        this.value = firstname + "." + lastname;
    }
} );

我研究了如何使用 jquery 选择器获取 Asp.net 控件 ID
("#<%=TextBox_username.ClientID%>")
我复制了给出的格式,所以没有错。 但它也不起作用。
如果出现问题,我不知道如何测试它。如果有人可以解释我如何确定这是否有效,我会很高兴。不过要是能看到解决方案就更好了

编辑: 我没有使用主页 - 内容占位符,我只使用默认的网络表单

试试这个 .aspx:

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <script src="jquery-1.11.3.js"></script> <!-- or 1.9 -->
    <script type="text/javascript">
        $(document).ready(function () {
            $("#<%=TextBox_username.ClientID%>").focus(function () {

                var firstname = $("#<%=TextBox_firstname.ClientID%>").val();
                var lastname = $("#<%=TextBox_lastname.ClientID%>").val();
                if (firstname && lastname && !this.value) {
                    this.value = firstname + "." + lastname;
                }
            });
        });

    </script>

</head>
<body>
    <form id="form1" runat="server">
        <div>
            <asp:TextBox ID="TextBox_firstname" runat="server" name="firstname"></asp:TextBox>
            <asp:TextBox ID="TextBox_lastname" runat="server" name="lastname"></asp:TextBox>
            <asp:TextBox ID="TextBox_username" runat="server" name="username"></asp:TextBox>
        </div>
    </form>
</body>
</html>

如果包含运算符 <%= 的文件以 .aspx

结尾,运算符 <%= 只会是 运行

具有 .js 扩展名的文件 不会 由 ASP.NET 引擎解析,因此 [=] 的代码相同11=] 将在浏览器的 .js 文件中呈现。 ASP.NET 不会处理 .js 文件并将此代码转换为字段的实际 ID (TextBox_firstname)

Mate 中的示例之所以有效,是因为 he/she 将 JavaScript 代码直接放入 .aspx 文件中。我怀疑您的 js 代码在 .js 文件中,而不是直接在 .aspx 文件中?

克雷格