Javascript 单击html中的链接不会';Don’不要把我转到正确的页面

Javascript 单击html中的链接不会';Don’不要把我转到正确的页面,javascript,html,asp.net-mvc,function,routes,Javascript,Html,Asp.net Mvc,Function,Routes,在过去的两个星期里,我一直在努力让它工作,但我似乎找不到问题。当我点击一个用户聊天时,它似乎并没有在两者之间创建一个聊天室。我遵循了那个教程,但编辑了一些东西,比如我的数据库。调试之后,当我点击其中一个用户链接时,它似乎什么都没做 我尝试过多次重命名所有内容并重新开始 我认为这可能与routeconfig有关,或者它甚至没有在单击时调用javascript gae 它所做的只是将我的当前地址:localhost/chat更改为localhost/chat 35; 我的javascript视图:

在过去的两个星期里,我一直在努力让它工作,但我似乎找不到问题。当我点击一个用户聊天时,它似乎并没有在两者之间创建一个聊天室。我遵循了那个教程,但编辑了一些东西,比如我的数据库。调试之后,当我点击其中一个用户链接时,它似乎什么都没做

我尝试过多次重命名所有内容并重新开始

我认为这可能与routeconfig有关,或者它甚至没有在单击时调用javascript gae

它所做的只是将我的当前地址:localhost/chat更改为localhost/chat 35;

我的javascript视图:

 // get chat data
    function getChat(contact_id) {
        $.get("/contact/conversations/" + contact_id)
            .done(function(resp) {
                let chat_data = resp.data || [];
                loadChat(chat_data);
            });
    }

    //load chat data into view
    function loadChat(chat_data) {
        chat_data.forEach(function(data) {
            displayMessage(data);
        });

        $('.chat__body').show();
        $('.__no__chat__').hide();
    }

    // select contact to chat with
    $('.user__item').click(function(e) {
        e.preventDefault();
        currentContact = {
            id: $(this).data('contact-id'),
            name: $(this).data('contact-name'),
        };
        if (conversationChannelName) {
            pusher.unsubscribe(conversationChannelName);
        }
        conversationChannelName = getConvoChannel((@ViewBag.currentUser.Id * 1), (currentContact.id * 1));
        currentconversationChannel = pusher.subscribe(conversationChannelName);
        bind_client_events();

        $('#contacts').find('li').removeClass('active');
        $('#contacts .contact-' + currentContact.id).find('li').addClass('active');
        getChat(currentContact.id);
    });

    function getConvoChannel(user_id, contact_id) {
        if (user_id > contact_id) {
            return 'private-chat-' + contact_id + '-' + user_id;
        }
        return 'private-chat-' + user_id + '-' + contact_id;
    }
Javascript:

 // get chat data
    function getChat(contact_id) {
        $.get("/contact/conversations/" + contact_id)
            .done(function(resp) {
                let chat_data = resp.data || [];
                loadChat(chat_data);
            });
    }

    //load chat data into view
    function loadChat(chat_data) {
        chat_data.forEach(function(data) {
            displayMessage(data);
        });

        $('.chat__body').show();
        $('.__no__chat__').hide();
    }

    // select contact to chat with
    $('.user__item').click(function(e) {
        e.preventDefault();
        currentContact = {
            id: $(this).data('contact-id'),
            name: $(this).data('contact-name'),
        };
        if (conversationChannelName) {
            pusher.unsubscribe(conversationChannelName);
        }
        conversationChannelName = getConvoChannel((@ViewBag.currentUser.Id * 1), (currentContact.id * 1));
        currentconversationChannel = pusher.subscribe(conversationChannelName);
        bind_client_events();

        $('#contacts').find('li').removeClass('active');
        $('#contacts .contact-' + currentContact.id).find('li').addClass('active');
        getChat(currentContact.id);
    });

    function getConvoChannel(user_id, contact_id) {
        if (user_id > contact_id) {
            return 'private-chat-' + contact_id + '-' + user_id;
        }
        return 'private-chat-' + user_id + '-' + contact_id;
    }
html


选择要聊天的联系人

    @foreach(var user in@ViewBag.allUsers) { }
路线图:

routes.IgnoreRoute(“{resource}.axd/{*pathInfo}”);
routes.MapRoute(
姓名:“家”,
url:“”,
默认值:新建{controller=“Home”,action=“Index”}
);
routes.MapRoute(
名称:“登录”,
url:“登录”,
默认值:新建{controller=“Auth”,action=“Index”}
);
routes.MapRoute(
名称:“聊天室”,
网址:“聊天室”,
默认值:新建{controller=“Chat”,action=“Index”}
);
routes.MapRoute(
名称:“GetContactConversations”,
url:“联系人/对话/{contact}”,
默认值:新建{controller=“Chat”,action=“ConversationWithContact”,contact=”“}
);
routes.MapRoute(
姓名:“PusherAuth”,
url:“推送/验证”,
默认值:新建{controller=“Auth”,action=“AuthForChannel”}
);
routes.MapRoute(
名称:“发送消息”,
url:“发送消息”,
默认值:新建{controller=“Chat”,action=“SendMessage”}
);
routes.MapRoute(
名称:“MessageDelivered”,
url:“message_delivered/{message_id}”,
默认值:新建{controller=“Chat”,action=“MessageDelivered”,message\u id=”“}
);
routes.MapRoute(
名称:“默认”,
url:“{controller}/{action}/{id}”,
默认值:新建{controller=“Auth”,action=“Index”,id=urlparmeter.Optional}
);

好的,如果什么都没发生,这听起来像是页面上的JS有问题。先排除这个可能性

你在JS中做过调试输出吗

首先,您可以包装您的点击事件,以便将其设置为文档就绪状态

更新-还添加在
部分
代码中,以确保在正确的时间执行所有操作(即,在加载JQuery捆绑包之后) 在你看来:

@section scripts {
    $(document).ready(function() {
       
        $('.user__item').click(function(e) {
            e.preventDefault(); 
            console.log("click registered!");   //also add this directly at the beginning of the click event so you can see if something happens (or not)
            //...your original code below... 
        });
    
    });
}
注意:
@section脚本…
在您的视图中只能出现一次

\u Layout.cshtml
文件中:

    ....
    @Scripts.Render("~/bundles/jquery")
    @Scripts.Render("~/bundles/bootstrap")
    @RenderSection("scripts", required: false)
</body>
</html>
。。。。
@Scripts.Render(“~/bundles/jquery”)
@Scripts.Render(“~/bundles/bootstrap”)
@RenderSection(“脚本”,必需:false)

这是您在上面发布的
Javascript
代码的一个变化。您正在包装
$('.user\uu item')。单击
$(文档)中的
代码。就绪(函数(){…})
以确保在将单击绑定到您发送的
html
代码中的项目之前已加载页面。在Chrome中,您可以通过右键单击页面,然后选择“inspect”ah ok来查看控制台-绘图变厚。从它的声音来看,您没有将jQuery链接起来。在您的
Shared/_Layout.cshtml
文件中,您应该有一个对JQuery的
引用。。。或者像这样的引用:
@Scripts.Render(“~/bundles/jquery”)
好的,在
/Scripts
中,您是否有
jquery xxxx.js
文件(其中“xxxx”是版本/缩小的变体)?同样在
/App\u Start/BundleConfig.cs
中,您应该声明jquery捆绑包(可能是第一个)。好的,太好了。好的,在
\u Layout.cshtml
中,在关闭
标记之前是否有
@RenderSection(“脚本”,必需:false)
?如果没有,请添加它。然后,在OP中的
Javascript
上,用我在上面的答案中所做的编辑将其包装起来……整个
标记都应该包装起来