【问题标题】:Impromptu - Simple pass variable to URL即兴 - 简单的将变量传递给 URL
【发布时间】:2014-12-29 20:08:50
【问题描述】:

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

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

【问题讨论】:

    标签: javascript jquery impromptu


    【解决方案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;
            }
        });
    }
    

    【讨论】:

    • 非常感谢。非常感谢您的帮助。干杯。保罗
    猜你喜欢
    • 2018-08-29
    • 2013-11-03
    • 1970-01-01
    • 2021-07-29
    • 2017-01-06
    • 1970-01-01
    • 2018-05-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多