开发者

jQuery advanced selector - select :selected from parent div

开发者 https://www.devze.com 2023-03-08 19:47 出处:网络
Can\'t get this selector to work... 开发者_JAVA百科$(\'div#associations_notes select#company :selected\').val()

Can't get this selector to work...

开发者_JAVA百科
$('div#associations_notes select#company :selected').val()

also tried..

$('div#associations_notes > select#company :selected').val()

and..

$('~ select#company :selected','div#associations_notes').val()

HTML:

<div id="associations_notes">
<div id="associated_companies_box"> 
<div id="company_associations_list"></div>
<div id="association_company" style="display:none;">
<select name="association_id" id=company class=works_for>
<option value="35">Compaq</option>

...

any thoughts?


If you only have one selected option, you should test the value of the select element, rather than finding the selected options. This will be computationally quicker (and quicker to code as well).

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

The selector can be reduced to the id selector because id properties must be unique to the page, so you almost certainly don't need to test its ancestors.


you just need to get the value of the option that was selected

$('#company option:selected').val();

  <select id="mySelect">
    <option>option 1</option>
    <option>option 2</option>
    <option>option 3</option>
    <option>option 4</option>
    <option>option 5</option>
    <option>option 6</option>
  </select>

It's the <option> that you need, not the <select> value

0

精彩评论

暂无评论...
验证码 换一张
取 消