简体   繁体   English

在经典ASP中呈现链接,然后使用jquery对其进行更改

[英]Rendering a link in classic ASP and then changing it with jquery

I have a link that i am rendering on page load using an if statement in classic asp, something like the following: 我有一个链接,该链接是我使用经典asp中的if语句在页面加载时呈现的,如下所示:

// SQL stuff up here

<% if not rs.eof then %>
    <a href="#" class="link" data-mediatype="on" data-id="<%=somevar%>"><i class="fa-star"></i></a>
<% else %>
    <a href="#" class="link" data-mediatype="off" data-id="<%=somevar%>"><i class="fa-star-o"></i></a>
<% end if %>

I'm also using a jQuery click event to post some of the data to an ajax request, like so; 我还使用jQuery click事件将一些数据发布到ajax请求中,如下所示;

$(document).ready(function(){

    $('.link').click(function(e) {

        e.preventDefault();

        var mediatype = $(this).data('mediatype');
        var id = $(this).data('id');

        $.ajax({
            url: '/post/url/here.asp',
            data: { 'media_type': mediatype, 'id': id },
            type: 'POST',
            success: function(result) {
                location.reload();
            }
        });

    });

});

If a record is found then the data-mediatype="on" and the <i> class="fa-star" , but if no record is found, then the data-mediatype="off" and the <i> class="fa-star-o" 如果找到记录,则data-mediatype="on"<i> class="fa-star" ,但是如果找不到记录,则data-mediatype="off"<i> class="fa-star-o"

Then when you click the link, it posts to the ajax. 然后,当您单击链接时,它将发布到ajax。

On success of the ajax call, the page reloads using location.reload() and it works. 成功执行ajax调用后,页面将使用location.reload()重新加载,并且可以正常工作。

Ideally I would like to do this without a page reload. 理想情况下,我希望在不重新加载页面的情况下执行此操作。 but im not sure how to go about it as the SQL needs to run to decide which link to show. 但由于SQL需要运行以确定要显示的链接,因此我不确定如何处理。

Any help would be greatly appreciated. 任何帮助将不胜感激。

I don't think you need to reload the whole page - just toggle the clicked link to be the other link (unless the click can change multiple links - it is unclear): 我认为您不需要重新加载整个页面-只需将单击的链接切换为另一个链接即可(除非单击可以更改多个链接-尚不清楚):

$('.link').click(function(e) {

        e.preventDefault();
        var $clicked = $(this),
            $icon = $clicked.children('i');

        var mediatype = $clicked.data('mediatype');
        var id = $clicked.data('id');

        $.ajax({
            url: '/post/url/here.asp',
            data: { 'media_type': mediatype, 'id': id },
            type: 'POST',
            success: function(result) {
              if (mediaType == 'on') {                /* swap icon and mediaType */
                $clicked.data('mediatype', 'off');
                $icon.removeClass('fa-star').addClass('fa-star-o');
              } else {
                $clicked.data('mediatype', 'on');
                $icon.removeClass('fa-star-o').addClass('fa-star');
              }
            }
        });

    });

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

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