Javascript 在click事件中获取元素的id,并使用id作为参数进行ajax调用
我有我的密码是Javascript 在click事件中获取元素的id,并使用id作为参数进行ajax调用,javascript,jquery,codeigniter,codeigniter-3,Javascript,Jquery,Codeigniter,Codeigniter 3,我有我的密码是 <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(); ?>/InchatroomCont
<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作为参数发送给控制器您的问题不够清楚。你的意思是当点击链接时,你需要发出一个ajax调用来调用控制器方法吗?我需要在点击标签a时获取id,如果我的答案有帮助的话,我需要将id发送给controllerSee
$(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
}
});
});
});