我想基于文本值更改表格循环中文本的颜色

时间:2018-09-10 01:20:51

标签: javascript php jquery css3

我有一个循环的table,显示下拉表单发布的结果(lotstatus)。我希望能够根据color中的value selected来更改文本的form

if($result = mysqli_query($conn, $sql)){ if(mysqli_num_rows($res) > 0){ echo "
<table class='table table-bordered table-striped' width='100%' data-pagination='true' data-search='true'>"; echo "
  <thead>"; echo "
    <tr>"; echo "
      <th>Status</th>"; echo "
      <th>Street #</th>"; echo "
      <th>Street Name</th>"; echo "
      <th>City</th>"; echo "
      <th>Parcel ID</th>"; echo "
      <th>Lot # (Unit)</th>"; echo "
      <th>Contract Signed Date</th>"; echo "
      <th>Closing Date</th>"; echo "
      <th>Action</th>"; echo "</tr>"; echo "</thead>"; echo "
  <tbody>"; while($row = mysqli_fetch_array($res)){ echo "
    <tr>"; echo "
      <td>" . "
        <div class='statuscolor'>". $row['lotstatus'] . "</div>". "</td>"; echo "
      <td>" . $row['streetnumber'] . "</td>"; echo "
      <td>" . $row['streetname'] . "</td>"; echo "
      <td>" . $row['city'] . "</td>"; echo "
      <td>" . $row['parcelid'] . "</td>"; echo "
      <td>" . $row['lotnumber'] . "</td>"; echo "
      <td>" . $row['contractsigneddate'] . "</td>"; echo "
      <td>" . $row['closingdate'] . "</td>"; echo "
      <td>"; echo "<a class='btn btn-info mr-2 mt-2' href='readLot.php?id=". $row[' id '] ."' title='View' data-toggle='tooltip'><span style='margin-left: 7px;' class='la la-file-o ks-icon'></span></a>"; echo "<a class='btn btn-warning mr-2 mt-2' href='updateLot.php?id=". $row['
          id '] ."' title='Edit ' data-toggle='tooltip'><span style='margin-left: 7px;' class='la la-pencil ks-icon'></span></a>"; echo "
        <a class='btn btn-danger mt-2' href='deleteLot.php?id=". $row[' id '] ."' title='Delete ' data-toggle='tooltip'><span style='margin-left: 7px;' class='la la-close ks-icon'></span></a>"; echo "</td>"; echo "</tr>"; } echo "</tbody>"; echo "</table>";
// Free result set mysqli_free_result($result); } else{ echo "
<p class='lead'><em>No Lots were found.</em></p>"; } }

这里是我使用的表格:用户选​​择批次状态-我想根据他们选择的内容更改状态的颜色。

<select class="form-control" value="<?php echo $lotstatus; ?>" id="lotstatus" name="lotstatus">
  <option value="NA">N/A</option>
  <option value="Executed">Executed</option>
  <option value="Not Interested">Not Interested</option>
  <option value="Hold">Hold</option>
  <option value="Title Ordered">Title Ordered</option>
  <option value="Vetting">Vetting</option>
  <option value="Survey Ordered">Survey Ordered</option>
  <option value="Lean Search Ordered">Lean Search Ordered</option>
  <option value="Clear to Close">Clear to Close</option>
  <option value="Closed">Closed</option>
</select>

我尝试设置选项值的样式,还尝试选择字段的text()值。什么都没用...帮助!

1 个答案:

答案 0 :(得分:0)

首先,您需要为每行添加状态:

<tr class=\"Executed\">"; echo "
<tr class=\"Hold\">"; echo "

使用JS,您可以将所选状态添加到父级<table>

$('select#lotstatus').on('change', function() {
  $('table').attr('status', $(this).val());
});

然后,在CSS中,您需要设置每种状态所需的颜色:

table[status="Executed"] tr.Executed .statuscolor { color: red; }
table[status="Hold"] tr.Hold .statuscolor { color: green; }
table[status="Closed"] tr.Closed .statuscolor { color: yellow; }

也许您需要在表中添加一个“ id”,以避免与其他表发生冲突。