普通网友 2013-12-20 17:55
浏览 33
已采纳

表行attr()问题

I'm stuck on a piece of code that calls a jQuery modul populated with a table row id. I'm about 90% sure that the error is within my jQuery code. Once I click on the button (class="view"), I get an undefined value instead of the row ID.

Thanks for any help!

Relevant section of leads_queue_a.php:

<section class="main">
     <h1>Lead Queue - Assigned Leads</h1>
     <div id="lead_wrapper_a"></div>
</section>

fill_leads_a.php:

$cond = array();
$params = array();

if ($_POST['id'] == '') {
    return;
}

if (isset($_POST['id']) && $_POST['id'] != '') {
    $userID = $_POST['id'];
}
if (!empty($userID)) {
    $cond[] = '`users`.`id` = ?';
    $params[] = "$userID";
}

$query = "SELECT `leads`.`id`, `leads`.`fname`, `leads`.`lname`, `leads`.`lead_type`, `leads`.`addr_street`, `leads`.`addr_city`, `leads`.`addr_zip`, `leads`.`phone`, `leads`.`phone_alt`, `leads`.`note`, `leads`.`created_by`, `leads`.`created` FROM `leads`,`users`,`leads_assignment`";

if (count($cond)) {
    $query .= ' WHERE ' . implode(' AND ', $cond);
}
$query .= ' AND `leads`.`id` = `leads_assignment`.`id_lead`'
        . ' AND `users`.`id` = `leads_assignment`.`id_user`';


$stmt = $db->prepare($query);
$stmt->execute($params);

//TABLE BUILDER
$lead = '';
$lead .= '<div class="leads">';
$lead .= '<table class="lead_table">';
$lead .= '<tr>';
$lead .= '<td>Customer</td>';
$lead .= '<td>Phone</td>';
$lead .= '<td>Lead Type</td>';
$lead .= '<td>Status</td>';
$lead .= '<td>Operations</td>';
$lead .= '</tr>';

foreach ($stmt->fetchAll(PDO::FETCH_OBJ) as $row) {
    $id = $row->id;
    $status = get_statusLast($id);
    $fname = $row->fname;
    $lname = $row->lname;
    $lead_type = $row->lead_type;
    $addr_street = $row->addr_street;
    $addr_city = $row->addr_city;
    $addr_zip = $row->addr_zip;
    $phone = $row->phone;
    $phone_alt = $row->phone_alt;
    $note = $row->note;
    $created_by = $row->created_by;
    $created = $row->created;

    $lead .= "<tr id='$id'>";
    $lead .= '<td>' . $fname . ' ' . $lname . '<br />' . $addr_street . '<br />' . $addr_city . ' ' . $addr_zip . '</td>';
    $lead .= '<td>' . $phone . '<br />' . $phone_alt . '</td>';
    $lead .= '<td>' . $lead_type . '</td>';
    $lead .= '<td>' . $status . '</td>';
    $lead .= '<td><button type="button" class="view" name="view">View Notes</button><br />'
            . '<button type="button" class="update" name="update">Update Status</button></td>';
}
$lead .= '</table>';
$lead .= '</div>';
$db = null;
print $lead;

Relevent section of modul.js:

$("#lead_wrapper_a").on('click', '.view', function() {
      alert($('tr', this).attr('id'));
});
  • 写回答

4条回答 默认 最新

  • dsfphczao23473056 2013-12-20 17:57
    关注

    Because your selction is incorrect. Use closest to get to the clicked buttons parent row (tr). Using the syntax $('tr', this) you are trying to find tr that are descendant of the button .view which is incorrect. You need to go upwards to get to the tr. this in the handler will be the button.

    alert($(this).closest('tr').attr('id'));
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作