开发者

jQuery get href initial value once before it changes

In my application I have an ajax call, and on success it appends some data to an existing links href.

This works great. The issue is, If I want to run the ajax call again and on success, append some different data, it is taking the href value + the new value from the previous ajax call, and than adding the new data after that.

I want it to add the data to the inital href value, before it was appended.

Below is my code: (I have the sample sample value being appended each time for testing purposes)

        //get next page link value, so we can add filter to url
    var next_link = $("a.next").attr("href");

$.ajax({
        type: "POST",
        url: "ajax_calls.php",
        data: "instrument="+selected.val()+"&filter=true",
        success: function(listing){$("#listings").html(listing);

$("a.next").attr("开发者_开发问答href", next_link + '&field=x');

},
        error: function(){alert(3);$("#error").text("Could not retrieve posts").fadeIn(300).delay(900).fadeOut(300)}
    });

Any help on this would be appreciated. Thank you


How about using jQuery's .data method?

$('#change').click(function(){
    var newData = Math.random() * 1000; // fake new data
    $('a').each(function(i,e){
        var oldHref = $(this).data('oldHref');
        if (!oldHref){
            var oldHref = $(this).attr('href');
            $(this).data('oldHref',oldHref);
        }
        $(this).attr('href',oldHref + '#' + newData);
    });
});

<a href="http://google.com">Hello, world!</a><br />
<input type="button" id="change" value="Change HREF" />

example

Store the old value in a data element, then reference before each new change.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜