在点击事件中获取元素的 ID,并使用 ID 作为参数进行 ajax 调用

Get id of the element on click event and make an ajax call with id as parameter

我有我的代码是

<a class="btn btn-outline-primary" id="1"  href="<?php echo site_url(); ?>/InchatroomController/LikeMessage/1" style=""> </a>
<a class="btn btn-outline-primary" id="2"  href="<?php echo site_url(); ?>/InchatroomController/LikeMessage/2" style=""> </a>
<a class="btn btn-outline-primary" id="3"  href="<?php echo site_url(); ?>/InchatroomController/LikeMessage/3" style=""> </a>
<a class="btn btn-outline-primary" id="4"  href="<?php echo site_url(); ?>/InchatroomController/LikeMessage/4" style=""> </a>

我需要在点击事件中获取标签的 ID 并进行 ajax 调用,将 ID 作为参数发送给控制器

$(document).ready(function() {
    $("a").click(function(event) {
        // Fetch the id of the element clicked
        var elemId = event.target.id;
        // Sending ajax call to server with id of the clicked element
        $.ajax({
                url: 'yourControllerUrl',
                type: 'POST',
                data: jQuery.param({ id: elemId}) ,
                contentType: "application/json; charset=utf-8",
                success: function (response) {
                    // success javascript code
                },
                error: function () {
                    // failure javascript code
                }
        }); 
    });
});