我有一个单元格中带有select选项类型的表。 我想使用复选框,这样当它被选中时,显示该行中的单元格。 我可以显示行中的其他值,但我不知道如何获取选择选项值。
null
$(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">
null
我假设您正在使用jQuery来获取选择字段的值:
$("#test1").val()
var values = $('#test1').val();
和值返回所选数据