简体   繁体   English

单击按钮时带有按钮的可单击表格行同时触发

[英]Clickable table row with button when clicking button triggers both

I have a table that is dynamically created.我有一个动态创建的表。 Each table row has onclick implemented.每个table row实现了onclick

Here is my piece of code for creating table rows:这是我创建表格行的一段代码:

foreach ($db_response as $row) {
        echo '<tr onclick="open_edit_task_modal(this)" id="row-' . $row['id'] . '" ';
        if (Status::is_in_progress(intval($row['status']))) {
            echo 'class="table-warning"';
        } else if (Status::is_finished(intval($row['status']))) {
            echo 'class="table-success"';
        }
        echo '>';

        echo '<th scope="row">' . $row['id'] . '</th>';
        echo '<td>' . $row['street'] . '</td>';
        echo '<td>' . $row['phone'] . '</td>';
        echo '<td>' . format_date($row['date']) . '</td>';
        echo '<td>' . $row['price'] . '</td>';
        echo '<td>' . $row['comment'] . '</td>';
        echo '<td>' . $row['operator'] . '</td>';
        echo '<td>' . Status::get_status_name_by_id($row['status']) . ' / ';

        if (Status::is_unassigned(intval($row['status']))) {
            echo '<button type="button" class="btn btn-primary btn-sm" onclick="update_task_status(this)">Accept</button>';
        } else if (Status::is_in_progress(intval($row['status']))) {
            $db_response = DB::select_user_by_id(intval($row['worker_id']));

            if ($db_response['name'] === $_SESSION['name'] && $db_response['surname'] === $_SESSION['surname']) {
                echo '<button type="button" class="btn btn-success btn-sm" onclick="update_task_status(this)">Finish</button>';
            } else {
                echo $db_response['name'] . ' ' . $db_response['surname'];
            }
        }

        echo '</td>';
        ...

As you can see my <tr> has onclick which is called open_edit_task_modal() .如您所见,我的<tr>onclick ,称为open_edit_task_modal() And my button that is positioned on that same <tr> has onclick which is called update_task_status() .我位于同一个<tr>上的button具有onclick ,称为update_task_status()

Everything works with button clicking and row clicking, no complaints there.一切都适用于按钮点击和行点击,没有抱怨。

The only problem that I have is that when I click button the <tr> is also clicked so I am getting both onclick s triggered.我唯一的问题是,当我单击button时, <tr>也被单击了,所以我同时触发了onclick

I would like to avoid that behavior.我想避免这种行为。 I currently have no idea how I can achieve such thing, please help.我目前不知道如何实现这样的事情,请帮忙。

You can use a javascript variable as flag, like您可以使用 javascript 变量作为标志,例如

stop_tr = 0;


function open_edit_task_modal(_this){
    if(stop_tr == 0){
        // your code ...
    }
}


function update_task_status(_this){
    stop_tr = 1;// stop runinng open_edit_task_modal function
    setTimeout("stop_tr=0;", 2000);// = 2sec , set flag back
    // your code ...
}

or use jQuery events with not selector instead of onclick attributes或使用 jQuery 事件而不是选择器而不是onclick属性

$("#table_id .btn.btn1").on("click",function(){
    // which add 'btn1' class on your button too
    alert("button1 clicked");
});

$("#table_id tr").not('.btn').on("click",function(){
    // run your tr event without all buttons
    alert("tr clicked");
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM