简体   繁体   English

根据每个表格行中的单元格值更改按钮文本 - Jquery

[英]Change button text based on cell value in each table row - Jquery

I have a HTML table that is showing data from DB.我有一个 HTML 表,它显示来自数据库的数据。 A [status] column shows the status of each record as "AVAILABLE", "BOOKED" or "ON-HOLD". [status] 列将每条记录的状态显示为“AVAILABLE”、“BOOKED”或“ON-HOLD”。 the last column of table is [ACTION] column which contains a BUTTON.表的最后一列是 [ACTION] 列,其中包含一个 BUTTON。 If the status of row = "Available" then button text should be "BOOK".如果 row 的状态 =“Available”,则按钮文本应为“BOOK”。 Likewise if the status = "BOOKED" or "ON-HOLD" the the button text should be "VIEW".同样,如果状态 =“BOOKED”或“ON-HOLD”,则按钮文本应为“VIEW”。 I have applied css successfully via jquery that changes the background-color of status column based on the text in the cell as mentioned above but the same code is not working for button text.我已经通过 jquery 成功应用了 css,它根据上面提到的单元格中的文本更改状态列的背景颜色,但相同的代码不适用于按钮文本。

HTML HTML

<tr id="data_row">       
   <td class="align-middle text-center"><?php echo $Project; ?></td>
   <td class="align-middle text-center"><?php echo $Sector; ?></td>
   <td class="align-middle text-center"><?php echo $Category ;?></td>
   <td class="align-middle text-center"><?php echo $Plot_title ;?></td>
   <td class="align-middle text-center"><?php echo $App_date ;?></td>
   <td class="align-middle text-center"><?php echo $Customer ;?></td>
   <td class="align-middle text-center" id= "status"><?php echo $Status ;?></td>
   <td class="align-middle text-center">
       <button type="button" name="edit" id="<?php echo $Plot_ID; ?>" class="btn btn-outline-primary btn-sm edit_data" data-toggle="modal" data-target="#modalform" data-backdrop="static" data-keyboard="false">Book</button>
   </td>
</tr>   

Jquery Jquery

$('#myTable tr#data_row td#status').each(function () {
    switch ($(this).text()) {

        case "Booked":
            $(this).css({"background-color":"#eeac88", "color":" #7a3b2e"}); // (WORKING FOR CSS)
            $(".edit_data").text("View"); // (NOT WORKING FOR TEXT CHANGE)
            break;

        case "on-Hold":
            $(this).css("background-color","#fefbd8");
            $(".edit_data").text("View");
            break;
    
        default:
            $(this).css({"background-color": "#c5d5c5", "color":"green"});
            $(".edit_data").text("Book");
            break;
    }
});

Try尝试

$(".edit_data").hmtl("View");

Or put a <span> inside the button to access it with.text() method或者在按钮内放一个<span>以使用 .text() 方法访问它

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

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