使用jQuery获取php输出结果的值


Issue getting value of php output results with jQuery

我试图获得特定循环元素的值。我基本上有一个记录列表,如果我点击一条记录,我希望能够获得该元素的status的值。status的值为二进制,可以是0或1。我遇到的问题如下:

-我创建的点击函数没有生成值,警报也没有显示。

-正如你在我的foreach循环中看到的,如果$status满足一定的条件,我输出$status_img。我需要的值实际上是$status

有没有人看到我能做什么来帮助我的问题?

foreach ($rows as $row) {
            $status = $row['status'];
            $class = $status != 0 ? 'status-nonzero' : '';
            if ($status == 0) {
                $status_img = '<img src="../icons/collection/x-sign.png" alt="Goal Not Complete">';
            }
            else {
                $status_img = '<img src="../icons/collection/checkmark.png" alt="Goal Complete">';
            }
            $goal_date = $row['date'];
            $fixed_goal_date = fixDate($goal_date);
            $html = "";
            $html .= '<div class="goal-box" id="comment-'.$row['id'].'">';
            $html .= '<div class="goal-box-left">';
            $html .= '<div class="goal-post-status">'.$status_img. '</div>';
jQuery:

 $('.goal-post-status').click(function (event) {
     var status = $(this).val;
     alert(status);
 });

你可以在data-attribute中设置状态,然后从jQuery读取它:

HTML

$html .= '<div class="goal-box" id="comment-'.$row['id'].'">';
$html .= '<div class="goal-box-left">';
$html .= '<div class="goal-post-status" data-status="'.$status.'">'.$status_img. '</div>';
Javascript

$("body").on("click", ".goal-post-status", function (event) {
    var status = $(this).attr("data-status");
    alert(status);
});

你可以使用:用$(this).html()代替$(this).val()

 $('body').on('click','.goal-post-status',function (event) {
     var status = $(this).html();
     alert(status);
 });

试试这个:

$(".goal-post-status").click(this, function() {
    var status = $(this).html();
    alert(status);
});