1

jQuery AJAX に渡す URL があります。

<a href="/wishlist.php?sku=C5&amp;action=move&amp;qty=1" class="buttoncart black">Move To Wishlist</a>;

AJAXに到達したら、href属性を次のように変更したい

<a href="/ajax_page.php?sku=C5&amp;action=move&amp;qty=1">Move blaf</a>

私はまだ初心者です。きっと簡単な方法があるはずです。これが私のスクリプトです。

var wishorder = {
    init: function(config){
        this.config = config;
        this.bindEvents();
    },
    bindEvents: function(){
        this.config.itemSelection.on('click',this.addWish);
    },
    addWish: function(e){
        console.log('working');
        console.log($(this).attr('href').pathname);
        var self = wishorder;

        $.ajax({
            //this is where im using the href and would like to change it
            //but i cant seem to access to get variables
            url: $(this).attr('href'),
            //url: '/ajax/ajax_move.php',
            type: 'GET',
            data: {
                sku: $(this).data('sku'),
                action: $(this).data('action'),
                qty: $(this).data('qty')
            },
            success: function(results){
                console.log(results);
                $('#cartcont').html(results);
            }
        });
        e.preventDefault();
    }
};
wishorder.init({
    itemSelection: $('#carttable tr a'),
    form: $('#cartfrm')
});
4

2 に答える 2

2

ロジックで使用replaceして、URL を変更できます。addWish

addWish: function(e){
    var self = wishorder;
    var url = $(this).attr('href').replace('wishlist.php', 'ajax_page.php');

    $.ajax({
        url: url ,
        type: 'GET',
        data: {
            sku: $(this).data('sku'),
            action: $(this).data('action'),
            qty: $(this).data('qty')
        },
        success: function(results){
            console.log(results);
            $('#cartcont').html(results);
        }
    });
    e.preventDefault();
}
于 2013-09-14T15:47:16.643 に答える
1

文字列の交換が必要です。

var original_href = $('<a href="/wishlist.php?sku=C5&amp;action=move&amp;qty=1" class="buttoncart black">').attr('href');

var new_href = original_href.replace(/wishlist.php/, "ajax_page.php");
于 2013-09-14T15:46:50.870 に答える