0

真的可以使用一些帮助/逻辑。

我想在 jquery 即兴的 href 上传递一个 elementid,然后可以使用它附加到目标 url。

那是; 以href开头:

<a class="link" href="javascript:;" elementid="66">Remove item</a>

我想要它,这样如果我单击此链接,它会将我发送到:remove-element.php?elementid=66

我的 javascript 看起来像:

<script>
function removeItem(id) { 
var txt = 'Are you sure you want to remove this item?';
$.prompt(txt, { 
buttons: {Yes: true, Cancel: false}, 
callback: function(accept) {
if (accept) {
window.location = "remove-element.php?elementid=x";
}
return false;
}
});
}

$('a.link').click(function() {
removeItem($(this).data('id'));
return false;
});

</script>
4

1 回答 1

1

为了使用$(this).data('id'),您需要将锚标记设置为:

<a class="link" href="javascript:;" data-elementid="66">Remove item</a>

并传入值,如下:

$('a.link').click(function() {
    removeItem($(this).data('id'));
    return false;
});

function removeItem(id) { 
    var txt = 'Are you sure you want to remove this item?';
    $.prompt(txt, { 
        buttons: {Yes: true, Cancel: false}, 
        callback: function(accept) {
            if (accept) {
                window.location.href = "http://your_site.com/remove-element.php?elementid=" + id;
            }
            return false;
        }
    });
}
于 2014-11-03T07:43:08.137 回答