1

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>

ご検討いただきありがとうございます。

4

3 に答える 3

2

選択オプション値を読み取るには

$('#grams').val();

選択オプション値を設定するには

$('#grams').val('newValue');

選択したテキストを読むには

$('#grams>option:selected').text();

それがうまくいくことを願っています。

于 2013-05-22T05:55:14.567 に答える
1

これはうまくいくはずです:

$("#grams").val();

これは簡単なフィドルです。

于 2013-05-22T05:53:44.483 に答える
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());
});
于 2013-05-22T06:06:25.570 に答える