简体   繁体   English

如何在JavaScript中执行回调+更新div标签

[英]How to do callback + update div tag in javascript

I have an ASP.NET MVC application with pages where the content is loaded into divs from client via javascript/jquery/json. 我有一个带有页面的ASP.NET MVC应用程序,其中的内容通过javascript / jquery / json从客户端加载到div中。 The loaded content contains a-tags with references to a function that updates server side vaules, then redirects to reload of entire page even though . 加载的内容包含a-tags,这些a-tags引用了一个功能,该功能可以更新服务器端的值,然后重定向到重新加载整个页面(即使)。

I wish to replace the a-tags with 'something' to still call a server-side function, then reload the div only. 我希望将a-tag替换为“ something”以仍然调用服务器端函数,然后仅重新加载div。

What is the 'right' way of doing this? 这样做的“正确”方法是什么?

Be gentle - I'm new to JS ;-) 保持温柔-我是JS的新手;-)

All comments welcome, 欢迎所有评论,

Anders, Denmark 丹麦安德斯

This is as far as I got so far. 这是我到目前为止所知道的。 getResponseCell() returns a td-tag filled with a-tag. getResponseCell()返回填充有a-tag的td-tag。 I've mangled Glens suggestion into the .click() addition, but it just calls the onClickedEvent... 我已经将Glens建议修改为.click()附加项,但它只是调用了onClickedEvent ...

Code sample: 代码示例:

    onClickedEvent=function()
    {
        return false;
    }

    getResponseCell=function(label, action, eventId)
    {
        tmpSubSubCell=document.createElement("td");
        link = document.createElement("A");
        link.appendChild( document.createTextNode( label));
        link.setAttribute("href", "/EventResponse/"+ action + "/" + eventId);
        //link.setAttribute("href", "#divContentsEventList");
        //link.setAttribute("onclick", "onClickedEvent(); return false;");
        link.setAttribute("className", "eventResponseLink");


        link.click(onClickedEvent());

        // link=jQuery("<A>Kommer<A/>").attr("href", "/EventResponse/"+ action + "/" + eventId).addClass("eventResponseLink");
        // link.appendTo(tmpSubSubCell);
        tmpSubSubCell.appendChild(link);

        return tmpSubSubCell;
    }

And the solution that worked for me looks like this: 适用于我的解决方案如下所示:

    onClickedEvent=function(event, actionLink)
    {
            event.preventDefault(); 

            $("eventListDisplay").load(actionLink);

            refreshEventList();                

            return false;
    }

    getResponseCell=function(label, action, eventId)
    {
        tmpSubSubCell=document.createElement("td");
        link = document.createElement("A");
        link.setAttribute("id",action + eventId);
        link.appendChild( document.createTextNode( label));
        actionLink = "/EventResponse/"+ action + "/" + eventId;
        link.setAttribute("href", actionLink);
        className = "eventResponseLink"+ action + eventId;
        link.setAttribute("className", className);

        $('a.'+className).live('click', function (event)
            {
                onClickedEvent(event,$(this).attr('href'));
            });

        tmpSubSubCell.appendChild(link);

        return tmpSubSubCell;
    }

We need code to give you a proper answer, but the following code will catch the click of an a-tag, and reload the div that it's inside: 我们需要代码来为您提供正确的答案,但是以下代码将捕获a-tag的点击,并重新加载其中的div:

$(document).ready(function() {
    $("a").click(function() {
        //call server-side function
        var parentDiv = $(this).parents("div:first");
        $(parentDiv).load("getContentOfThisDiv.asp?id=" + $(parentDiv).attr("id"));
    });
});

In the above code, when a link is clicked, the div that this the link is inside will be loaded with the response of the call to the asp file. 在上面的代码中,当单击链接时,该链接位于其中的div将被加载对asp文件的调用响应。 The id of the div is sent to the file as a parameter. div的id作为参数发送到文件。

Without really seeing more information..... 没有真正看到更多的信息.​​....

If you're a's are being added to the DOM after the initial page load, you cannot use the usual click() or bind() methods in jQuery; 如果您是在初始页面加载后添加到DOM中的,则不能在jQuery中使用通常的click()bind()方法; this is because these methods only bind the events to those elements that are registered in the DOM at the time the methods are called. 这是因为这些方法仅将事件绑定到在调用方法时在DOM中注册的那些元素。 live() on the other hand, will register the event for all current, and future elements (using the event bubbling mechanism in Javascript). 另一方面, live()将为所有当前和将来的元素注册事件(使用Javascript中的事件冒泡机制)。

$(document).ready(function () {
    $('a.eventResponseLink').live('click', function (event) {
        var self = $(this);

        self.closest('div').load('/callYourServerSideFunction.asp?clickedHref=' + self.attr('href'));

        event.preventDefault();
    });
});

We're using event.preventDefault() to prevent the default action of the a-tag being executed; 我们使用event.preventDefault()来防止执行a-tag的默认操作; eg reloading or changing page. 例如重新加载或更改页面。

Edit: The issue won't be caused by that. 编辑:问题将不会由此引起。 That's the power of jQuery; 这就是jQuery的功能; being able to bind the same event to multiple elements. 能够将同一事件绑定到多个元素。 Check your HTML; 检查您的HTML; maybe you're missing a closing </a> somewhere? 也许您在某处缺少</a> Maybe your binding the event in a location that gets called multiple times? 也许您将事件绑定到多次调用的位置? Each time .live() gets called, it will add ANOTHER event handler to all matched elements. 每次调用.live()时,它将向所有匹配的元素添加ANOTHER事件处理程序。 It only needs to be bound once on page load. 页面加载只需绑定一次。

jQuery provides loads of way for you to select the elements; jQuery为您提供了选择元素的方式。 check out the list . 查看清单 Looking at your link variable, it looks like all your links have a href starting with /EventResponse/ ; 查看您的链接变量,您所有的链接似乎都有一个以/EventResponse/开头的href。 so you can use $('a[href^=/EventResponse/]') as the selector instead. 因此您可以使用$('a[href^=/EventResponse/]')作为选择器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM