简体   繁体   English

如何根据Javascript中的“类”选择器检索onclick事件的href值?

[英]how to retrieve href value for an onclick event based on “class” selector in Javascript?

I have我有

<a href="test.php?id=2" class="confirm_delete">Click to proceed</a>

and following javascript.并遵循javascript。 When I click on above link it displays the dialog box with 2 buttons.当我单击上面的链接时,它会显示带有 2 个按钮的对话框。 "return false;" “返回假;” stops the default event of link tag.停止链接标签的默认事件。 But I need the functionality in which when I click "Yes, delete" to take me to other page by choosing href value of a onclicked anchor.但是我需要这样的功能:当我单击“是,删除”时,通过选择点击锚点的 href 值将我带到其他页面。 I tried alert($(this).attr('id'));我试过 alert($(this).attr('id')); (as I thought I could pick up HREF value using "this.attr('href')") but it displays "dialog". (因为我认为我可以使用“this.attr('href')”来获取 HREF 值)但它显示“对话框”。

How do I make it work so that when I click on a link having class name "confirm_delete" it displays me dialog, and if I click cancel it stays on the page otherwise takes me to page according to href value.我如何使它工作,以便当我单击类名为“confirm_delete”的链接时,它会显示我的对话框,如果我单击取消,它会留在页面上,否则会根据 href 值将我带到页面。

$(".confirm_delete").click(function(){
    $('<div id="dialog">Are you sure you want to delete?</div>').appendTo('body');
    $("#dialog").dialog({
        bgiframe: true, resizable: false, height:140, modal: true,
        overlay: {
            backgroundColor: '#000', opacity: 0.5
        },
        buttons: {
            'Yes, Delete all items in recycle bin': function() {
                $(this).dialog('close');
                $(this).remove();
                alert($(this).attr('id'));
            },
            Cancel: function() {
                $(this).dialog('close');
                $(this).remove();
            }
        }
    });
    return false;
    });

Thank you.谢谢你。

First off: try not to use underscores in class names.首先:尽量不要在类名中使用下划线。 I've read somewhere they may cause problemsn...我在某处读过它们可能会导致问题...

Well, here:嗯,在这里:

$('a.confirm-delete').click( function(event) {
    if( !confirm('are you sure you want to go?') ) return false;
});

here I've usedd the javascript confirm dialog.在这里,我使用了 javascript 确认对话框。 You can easily replace that with a jquery modal dialog.您可以轻松地将其替换为 jquery 模式对话框。

jrh jrh

OK, I can retrieve value of HREF by var url = $(this).attr('href');好的,我可以通过var url = $(this).attr('href');检索 HREF 的值 just after the $(".confirm_delete").click(function(){ line. It gives me "test.php?id=123" Only thing left is create/retrieve full url and redirecting to that url.就在 $(".confirm_delete").click(function(){ 行之后。它给了我 "test.php?id=123" 唯一剩下的就是创建/检索完整的 url 并重定向到该 url。

Thanks谢谢

This should do the trick:这应该可以解决问题:

$(".confirm_delete").click(function(){
       var $delete = $(this);
       ...
        $("#dialog").dialog({
                ...
                buttons: {
                        'Yes, Delete all items in recycle bin': function() {
                                $(this).dialog('close');
                                $(this).remove();
                                alert($delete.attr('id'));
                        }
                }
        });
        return false;
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM