gpt4 book ai didi

javascript - jquery获取 anchor 标签的data-id

转载 作者:行者123 更新时间:2023-11-30 16:11:34 26 4
gpt4 key购买 nike

我从 jQuery 创建了下表:

$('#btniLoad').click(function(){
var gradelevel = $('#ylvl').val();

$.ajax({
type:'POST',
url:"<?php echo site_url('enrollment/searchTeacher');?>",
dataType:'json',
data:{q:gradelevel},
success: function(data){

if(!data.teacherinfo[0])
{
var table = "There's no teacher registered yet";

}
else
{
var table = "<table class='table table-striped table-bordered' cellspacing='0' width='100%'>";
table += "<thead><th>Teacher's Name</th></thead><tbody>";
for(var i = 0; i < data.teacherinfo.length;i++)
{
table += "<tr><td><a href='#teachermodal' data-toggle='modal' role='button' id='teachview' data-id='"
+data.teacherinfo[i].teacherid+"'>"
+data.teacherinfo[i].FirstName+', '+data.teacherinfo[i].MiddleName+', '
+data.teacherinfo[i].LastName+"</a></td></tr>";
}
table += "</tbody></table>";
}

$('#teacherTable').empty();
$('#teacherTable').append(table);
},
});
});

我想在控制台日志中显示 href 中的 data-id 以检查它是否正确传递了 id 但我没有从控制台日志中得到任何结果.... 这是 anchor 标记的点击事件:

$(function(){
$('#teachview').click(function(){
var teachid = $(this).attr("data-id");
console.log(teachid);
});
});

最佳答案

使用 $(document).on('click','#teachview',function(){ 而不是 $('#teachview').click(function(){

ID 应该是唯一的使用类代替

关于javascript - jquery获取 anchor 标签的data-id,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36168629/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com