简体   繁体   English

jQuery:如何获取表行中所选选项的值

[英]jQuery: how to get value of selected option in a table row

Here is the part of the DOM I'm working with: 这是我正在使用的DOM的一部分:

<tr id="player-row-973">
    <td class="display_name"> Kevin Love </td>
    <td class="position">
    <select id="position" name="position">
      <option value="pf">PF</option>
      <option value="c">C</option>
    </select>
   </td>
</tr>

Using jQuery, I'm trying to get the value of the position currently selected for the player Kevin Love. 我正在尝试使用jQuery获取球员凯文·洛夫(Kevin Love)当前所选位置的value I've tried using something such as the following: 我尝试使用以下内容:

$('#player-row-973').find('#position').value() but that doesn't seem to do the trick. $('#player-row-973').find('#position').value()但这似乎没用。

关闭,其.val()

$('#player-row-973').find('#position').val()

I use this: 我用这个:

$('#player-row-973').find('#position option:selected').val();

For some reasons it does not always succeed with $('#selectId').val() . 由于某些原因,它并不总是以$('#selectId').val()

firstly if you use ID you can directly select it like: 首先,如果您使用ID,则可以直接选择它,例如:

$('#position')

and get its value like: 并获得其值,例如:

$('#position').val()

but if you want to have multiple select options like this, you better remove the id attribute and use class or name , then you have all these alternatives to get the value: 但是,如果您希望拥有多个这样的选择选项,则最好删除id属性并使用classname ,然后使用所有这些替代方法来获取值:

using id attribute: 使用id属性:

$('#position').val();
$('#position>option:selected').val();

using name attribute: 使用name属性:

$('#player-row-973 select[name=position]').val()
$('#player-row-973 select[name=position]>option:selected').val()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM