$(document).ready(...) 不会被调用

发布于 2024-12-26 13:23:30 字数 2682 浏览 0 评论 0原文

我有以下 ejs 模板

<!doctype html>
<html>
    <head>
        <meta charset="utf-8">
        <title>Emasc Editor</title>
        <style>
            .list{
                display: inline-block;
            }
        </style>
        <script type="text/javascript" src="/js/jquery-1.7.1.min.js"></script>
        <script type="text/javscript">
$(document).ready(function(){
    alert('test');
    $("#setMembershipsButton").click(function(e){
        // send selected person and groups to server and create memberships
        data = {
            person: $('#person_list').val(),
            groups: $('#group_list').val()
        };
        $.post('/newConnections', data);
    });
    $("#person_list").change(function(){
        alert('test');
        // send selected person to server and retrieve memberships
        $.post('/getPersonGroups', { personID: $('#person_list').val() }, function(data, textStatus){
            $('#group_list option').removeAttr('selected');
            for(var i=0; i<data.ids.length; i++){
                $('#group_list option[value=' + data.ids[i] + ']').attr('selected', 'selected');
            }
        });
    });
});
        </script>
    </head>
    <body>
        <div class="list">
            <select id="person_list" size="<%= persons.length+1 %>">
                <% for( var i=0; i<persons.length; i++){ %>
                    <option value="<%= persons[i].id %>"><%= persons[i].name %></option>
                <% } %>
            </select>
            <form method="post" action="/newPerson">
                <input type="text" name="user[name]" />
                <input type="submit" value="Submit" />
            </form>
        </div>

        <div class="list">
            <select id="group_list" multiple size="<%= groups.length+1 %>">
                <% for( var i=0; i<groups.length; i++){ %>
                    <option value="<%= groups[i].id %>"><%= groups[i].name %></option>
                <% } %>
            </select>
            <form method="post" action="/newGroup">
                <input type="text" name="group[name]" />
                <input type="submit" value="Submit" />
            </form>
        </div>

        <br/>

        <input id="setMembershipsButton" type="button" value="Set Memberships"></input>
    </body>
</html>

并且第一个警报('test')永远不会被调用。 我在这里缺少什么?

I have the following ejs template

<!doctype html>
<html>
    <head>
        <meta charset="utf-8">
        <title>Emasc Editor</title>
        <style>
            .list{
                display: inline-block;
            }
        </style>
        <script type="text/javascript" src="/js/jquery-1.7.1.min.js"></script>
        <script type="text/javscript">
$(document).ready(function(){
    alert('test');
    $("#setMembershipsButton").click(function(e){
        // send selected person and groups to server and create memberships
        data = {
            person: $('#person_list').val(),
            groups: $('#group_list').val()
        };
        $.post('/newConnections', data);
    });
    $("#person_list").change(function(){
        alert('test');
        // send selected person to server and retrieve memberships
        $.post('/getPersonGroups', { personID: $('#person_list').val() }, function(data, textStatus){
            $('#group_list option').removeAttr('selected');
            for(var i=0; i<data.ids.length; i++){
                $('#group_list option[value=' + data.ids[i] + ']').attr('selected', 'selected');
            }
        });
    });
});
        </script>
    </head>
    <body>
        <div class="list">
            <select id="person_list" size="<%= persons.length+1 %>">
                <% for( var i=0; i<persons.length; i++){ %>
                    <option value="<%= persons[i].id %>"><%= persons[i].name %></option>
                <% } %>
            </select>
            <form method="post" action="/newPerson">
                <input type="text" name="user[name]" />
                <input type="submit" value="Submit" />
            </form>
        </div>

        <div class="list">
            <select id="group_list" multiple size="<%= groups.length+1 %>">
                <% for( var i=0; i<groups.length; i++){ %>
                    <option value="<%= groups[i].id %>"><%= groups[i].name %></option>
                <% } %>
            </select>
            <form method="post" action="/newGroup">
                <input type="text" name="group[name]" />
                <input type="submit" value="Submit" />
            </form>
        </div>

        <br/>

        <input id="setMembershipsButton" type="button" value="Set Memberships"></input>
    </body>
</html>

And the first alert('test') never gets called.
What am I missing here?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

绝影如岚 2025-01-02 13:23:30

您的脚本类型无效:

<script type="text/javscript">

<script type="text/javascript">

来自 Mozilla

类型
此属性标识嵌入在脚本元素中或通过元素的 src 属性引用的代码的脚本语言。这被指定为 MIME 类型;支持的 MIME 类型的示例包括 text/javascript、text/ecmascript、application/javascript 和 application/ecmascript。如果缺少此属性,则脚本将被视为 JavaScript。

但是,如果浏览器无法识别该类型,则该块将被忽略。最好完全省略 type 属性,以避免输入错误。

Your script type is invalid:

<script type="text/javscript">

should be

<script type="text/javascript">

From Mozilla:

type
This attribute identifies the scripting language of code embedded within a script element or referenced via the element’s src attribute. This is specified as a MIME type; examples of supported MIME types include text/javascript, text/ecmascript, application/javascript, and application/ecmascript. If this attribute is absent, the script is treated as JavaScript.

However, if the type is not recognized by the browser, the block is ignored. It's best to leave out the type attribute entirely to avoid mistyping it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文