jquery获取锚标记的data-id

时间:2016-03-23 02:11:26

标签: javascript jquery html

我有从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,但是我没有从控制台日志中获得任何结果....  这是锚标记的click事件:

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

1 个答案:

答案 0 :(得分:2)

使用$(document).on('click','#teachview',function(){代替$('#teachview').click(function(){

ID应该是唯一的 使用