我需要使用jquery从表中获取所选值我没有得到任何值,请帮助我
这是我的代码:
$(this).parents("tr:first").find("td:nth-child(1).option:selected").text();
<td>
<select id="grams">
<option value=""></option>
<option value="100">100g</option>
<option value="250">250g</option>
<option value="500">500g</option>
</select>
</td>
感谢您对此进行调查。
答案 0 :(得分:2)
阅读选择选项值使用
$('#grams').val();
设置选择选项值使用
$('#grams').val('newValue');
要阅读所选文本,请使用
$('#grams>option:selected').text();
希望它有效。
答案 1 :(得分:0)
答案 2 :(得分:0)
尝试使用这些,因为您已经为元素指定了一个id,您可以直接从中获取值:
$('#grams').val(); // gets the current option value
$('#grams option:selected').val(); // gives you the selected option value
如果您希望它通过绑定change
事件:
// with a selector's context way
$('#grams').on('change', function(e){
console.log($('option:selected', this).val());
});
// with find() method of jQuery
$('#grams').on('change', function(e){
console.log($(this).find('option:selected').val());
});