获取表格单元格的选择选项值

时间:2020-06-07 12:21:30

标签: javascript jquery

我在一个单元格中有一个带有选择选项类型的表。我想使用复选框,以便在选中该复选框时显示此行中的单元格。我可以在行中显示其他值,但是我不知道如何获取选择选项值。

$(document).ready(function() {
  $("#saverecord").click(function(event) {
    var currentRow = $(".btnSelect:checked").closest("tr");
    var col1 = currentRow.find("td:eq(0)").text();
    var col2 = currentRow.find("td:eq(1)").text();
    var col3 = currentRow.find("td:eq(2)").text();
    var data = col1 + "\n" + col2 + "\n" + col3;
    console.log(data);
    alert(data);
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table name="mytable" id="mytable">
  <tr>
    <th>year</th>
    <th>item</th>
    <th>type</th>
    <th>checked</th>
  </tr>
  <tr>
    <td>2020</td>
    <td>shoe</td>
    <td>
      <select name="test1" id="test1">
        <option value="A">A</option>
        <option value="B">B</option>
        <option value="C">C</option>
      </select>
    </td>
    <td><input type="checkbox" class="btnSelect" id="chk" name="chk" /></td>
  </tr>
</table>
<input type="button" value="Save the record" id="saverecord" class="button0">

1 个答案:

答案 0 :(得分:0)

假设您使用的是jQuery,以获取选择字段的值:

$("#test1").val()