gpt4 book ai didi

javascript - 即兴 - 简单地将变量传递给 URL

转载 作者:行者123 更新时间:2023-12-03 11:31:33 25 4
gpt4 key购买 nike

确实需要一些帮助/逻辑。

我想在 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>

最佳答案

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

<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;
}
});
}

关于javascript - 即兴 - 简单地将变量传递给 URL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26709671/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com