使用JQuery从属性获取值

时间:2014-01-15 21:06:26

标签: javascript jquery

我的页面中有一个包含许多行的表,每行都有一个<a href>的图像<a href>有两个属性类和data-delete-id,所以我有一个多个<a href>具有相同的属性和类的相同值。

每个<a href>都有data-delete-id属性的不同值。

在我的JavaScript中,我有这段代码:

<script>
        $(function() {
            $('.deleteButton').confirmOn('click', function(e, confirmed){
                // get jquery object access to the button
                var $thisButton = $(this);
                // this gets that data directly from the HTML
                var id = $thisButton.data('delete-id');
                if(confirmed) {
                    alert("<?php echo site_url('users/deleteUser')?>" + '/' + id);
                }

            });

        });

</script>

因此,当我点击某个<a href>时,该功能会被触发,并且它会从id属性中获取data-delete-id值。

问题在于id值始终是我的HTML代码中第一个<a href>的值。

我只想获取所点击的data-delete-id的{​​{1}}属性的值。

我该怎么做?

1 个答案:

答案 0 :(得分:2)

我检查了confirmOn here的代码,似乎没有注册一组选定元素中的每个项目。这是插件的一个错误,但解决方法是将整个内容包装在each中。

    $(function() {
        $('.deleteButton').each(function() {
            $(this).confirmOn('click', function(e, confirmed){
               // get jquery object access to the button
               var $thisButton = $(this);
               // this gets that data directly from the HTML
               var id = $thisButton.data('delete-id');
               if(confirmed) {
                 alert("<?php echo site_url('users/deleteUser')?>" + '/' + id);
               }
            });
        });
    });
相关问题