从动态链接ID获取价值

时间:2018-03-13 13:36:47

标签: php jquery

我根据MySQL的结果生成动态ID,这样做正常。但是,我需要获得id的值以及我尝试的任何东西,它要么是未定义的,要么是空白的。如果有人能指出我的错误,我将不胜感激。感谢

<?php
    while ($inboxrow = mysqli_fetch_assoc($inboxresult)) {
     $id = $inboxrow["id_frm"];
     $message = $inboxrow["message_frm"];
     $date = $inboxrow["date_frm"];
     $date = strtotime( $date );
     $date = date( 'd/m/Y H:i:s', $date );
     $from = $inboxrow["from_frm"];
     $subject = $inboxrow["subject_frm"];

    echo '<div class="msgHdr" style="font-size: 13px; margin-right: 20px; margin-bottom: 10px; padding: 7px; border: 1px solid #ccc; height: auto; background: #fcfaf7">';
    echo '<span style="font-weight: bold;">' . $from . '</span>';
    echo '<br />';
    echo "<a class='dynid' href='#' id='" .$id. "'>" .$subject. "</a>" . '<span style="float: right; font-size: 12px;">' .$date. '</span>';
    echo '</div>';

?>

JS

$(function() {
  $(document).on('click', '.msgHdr', function() {
    //console.log(id);
    var anc = $(".dynid").find("#id").text();
    console.log(anc);
  });
});

2 个答案:

答案 0 :(得分:3)

查看您需要的代码

$(document).on('click', '.msgHdr', function() {
  var $link = $(this).find(".dynid"); 
  var subject = $link.text(); 
  var id = $link.attr("id");
});

答案 1 :(得分:1)

你应该做的事情如下:


    $(function() {
        $( ".msgHdr" ).click(function() {
            var anc = $(this).find(".dynid").text();
            console.log(anc);
        });
    });