选择具有相同类的多个元素

时间:2013-06-08 06:30:46

标签: jquery jquery-ui class select

我使用jQuery UI对话框插件显示一个确认框,用于删除相应的span。这是我的JS代码:

 $("#dialog-stergelu").dialog({ 
        autoOpen: false, 
        bgiframe: true, 
        resizable: false, 
        width: 400, 
        modal: true, 
        overlay: { 
            backgroundColor: '#000', 
            opacity: 0.5 
        }, 
        buttons: { 
            'Sterge': function() { 
                     window.location = $('.opener').attr('confirm');
             }, 
             'Anuleaza': function() { 
                      $(this).dialog('close'); 
              } 
         } 
  }); 
  $('.opener').click(function() { 
         $('#dialog-stergelu').dialog('open'); 
  }); 

这是HTML:

<div id="dialog-stergelu" title="Stergeti acest raport ? ">
   <p>
      <span class="ui-icon ui-icon-alert" style="float: left; margin: 0 7px 20px 0;"> </span>Chiar doriti sa stergeti acesta lucrare ? (Apasati "Anuleaza" pentru anularea comenzi)
   </p>
</div>
<span class="opener" confirm="delete.php?id=213115">Delete</span>
<span class="opener" confirm="delete.php?id=22314">Delete</span>
<span class="opener" confirm="delete.php?id=222111">Delete</span>
<span class="opener" confirm="delete.php?id=215231">Delete</span>
<span class="opener" confirm="delete.php?id=223151">Delete</span>

我想从点击的confirm获取at span。现在它只得到第一个。

5 个答案:

答案 0 :(得分:1)

您可以尝试将网址存储在此data元素的$("#dialog-stergelu")中,然后将其打开。像这样:

$('.opener').click(function() { 
    var url= $(this).attr("confirm"); //really, why wont this work?
    $('#dialog-stergelu').data('url', url).dialog('open'); 
});

然后,在对话框的buttons选项

buttons: { 
    'Sterge': function() { 
        window.location = $(this).data("url");
    }, 
    'Anuleaza': function() { 
        $(this).dialog('close'); 
    } 
} 

这是一个有效的演示:http://jsbin.com/edudun/4/edit

答案 1 :(得分:0)

尝试替换此行:

window.location = $(this).attr('confirm');

用这个:

window.location = $('.opener').attr('confirm');

答案 2 :(得分:0)

设置window.location后,浏览器会更改网址,页面上的脚本将停止执行。

答案 3 :(得分:0)

我认为您的问题是,您希望获得点击的span是吗?而不是错误地选择第一个匹配的$('.opener')是吗?

您需要将点击回调修改为:

$('.opener').click(function(e) {  
    // jqEl is your specfic jQuery span.opener that was clicked.
    // You can pass it to the dialog using any number of methods.
    var jqEl = $(e.target); 

    ('#dialog-stergelu').dialog('open'); 
}); 

答案 4 :(得分:0)

Try this code...

$("#dialog-stergelu").dialog({ 
    autoOpen: false, 
    bgiframe: true, 
    resizable: false, 
    width: 400, 
    modal: true, 
    overlay: { 
        backgroundColor: '#000', 
        opacity: 0.5 
    }, 
    buttons: { 
        'Sterge': function() { 
            window.location = $('#dialog-stergelu').data('confirm');
         }, 
         'Anuleaza': function() { 
             $(this).dialog('close'); 
         } 
    } 
}); 
$('.opener').click(function() { 
    $('#dialog-stergelu').data('confirm',$(this).attr('confirm'));
    $('#dialog-stergelu').dialog('open'); 
});