这是我想要做的。
在第一行中有tds,然后是a = the text in the first cell
和b = the selected value of the drop down that the user selected
我如何执行此操作,因为我的代码无效?
$(document).ready(function () {
var s = $('table td:first').parents('tr');
var a = s.eq(0).text();
var b = s.eq(1).find('option:selected').val();
alert(a + " " + b);
});
<table>
<tbody>
<tr>
<th>ID</th>
</tr>
<tr>
<td>
test
</td>
<td>
<select>
<option value="yes">yes</option>
<option selected="selected" value="no">no</option>
</select>
</td>
</tr>
</tbody>
</table>
答案 0 :(得分:2)
工作演示 http://jsfiddle.net/snU97/
休息随意玩耍,&amp;希望它能帮助您满足您的需求:)
代码
$(document).ready(function () {
var s = $('table td:first').parents('tr');
var a = s.find('td').eq(0).text();//s.eq(0).text();
var b = s.find('td').eq(1).find('option:selected').val();
alert(a + " " + b);
});
答案 1 :(得分:1)
您还可以使用以下代码
$(document).ready(function(){
var s = $('table td:first');
var a = s.html();
var b = s.parents('tr').children().find('option:selected').val();
alert(a + " " + b);
});
答案 2 :(得分:1)
有一个选项:
<table>
<tbody>
<tr>
<th>ID</th>
</tr>
<tr>
<td>
test
</td>
<td>
<select id="mydropdown">
<option value="yes">yes</option>
<option selected="selected" value="no">no</option>
</select>
</td>
</tr>
</tbody>
</table>
$(document).ready(function () {
var s = $('table td:first');
var a = s.text();
var b = $("#mydropdown option:selected").text();
alert(a + " " + b);
});