简体   繁体   English

如何使用它的类点击自我链接的锚标签?

[英]How to get the click on self linked anchor tag using its class?

I am trying to get the value of ID attribute of the anchor tag when it is clicked . 我试图在单击时获取标记的ID属性的值。

<a href="#" id="a'.$id.'" class="delete_assessor delete-row"><i class="fa fa-trash-o"></i></a>

<script type="text/javascript">
    $(document).ready(function(){

         $(".delete_assessor").live("click",function(){
            alert('delete');
            var id = $(this).attr("id");
            var trhandler = 'a'+id;


            if(confirm("Do you really want to delete this assessor ?")){
                ajax("delete_assessor",$(this).attr("id"),trhandler);

            }
        });
});

Am not able to alert any values and in console it is not showing any message 我无法提醒任何值,并且在控制台中它没有显示任何消息

alert('delete');
console.log('clicked');

As of jQuery 1.7, the .live() method is deprecated. 从jQuery 1.7开始,不推荐使用.live()方法。 Use .on() to attach event handlers. 使用.on()附加事件处理程序。 Also add event.preventDefault() for preventing default behavior or click event. 还要添加event.preventDefault()以防止默认行为或单击事件。

 $(document).ready(function() { $(".delete_assessor").on("click", function(e) { e.preventDefault() alert('delete'); var id = $(this).attr("id"); var trhandler = 'a' + id; if (confirm("Do you really want to delete this assessor ?")) { ajax("delete_assessor", $(this).attr("id"), trhandler); } }); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <a href="#" id="a'.$id.'" class="delete_assessor delete-row"><i class="fa fa-trash-o"></i>ggg</a> 

That is because .live() is deprecated in version 1.7 and removed in version 1.9. 这是因为.live()在1.7版中已弃用,在1.9版中已删除。 You should use .on() instead of .live() 您应该使用.on()而不是.live()

$(".delete_assessor").on("click",function(){
   //rest code....
});

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

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