最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to get selected option element attr in jQuery? - Stack Overflow

programmeradmin0浏览0评论

I want to get attribute value of selected option element.

jQuery:

$("#select_element").val().attr("val");

HTML:

 <select id="select_element" class="selectpanel">
     <option val="x">element1</option>
     <option val="y">element2</option>
 </select>

I want to get attribute value of selected option element.

jQuery:

$("#select_element").val().attr("val");

HTML:

 <select id="select_element" class="selectpanel">
     <option val="x">element1</option>
     <option val="y">element2</option>
 </select>
Share Improve this question edited Oct 12, 2018 at 10:15 Sivaprakash 4731 gold badge8 silver badges22 bronze badges asked Nov 6, 2014 at 12:47 HectorHector 1231 gold badge3 silver badges9 bronze badges 5
  • Remove the attr method and use value attribute not val. – Ram Commented Nov 6, 2014 at 12:50
  • 1 I Googled the title of your question and found the answer to your problem, you should do the same. stackoverflow./questions/2230704/… – fisk Commented Nov 6, 2014 at 12:50
  • possible duplicate of How do you select a particular option in a SELECT element in jQuery? – kockburn Commented Nov 6, 2014 at 12:53
  • val is not a valid attribute for option elements. The attribute is value. If you want a custom attribute, use a data- prefix. – T.J. Crowder Commented Nov 6, 2014 at 12:55
  • T.J. Crowder thanks for information – Hector Commented Nov 6, 2014 at 13:14
Add a ment  | 

4 Answers 4

Reset to default 4

Try this:

$('#select_element option:selected').attr('val');

Or

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

both are valid!

Simple use:

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

If you want the text of it, then:

$('#select_element option:selected').text();

JSFIDDLE

try this for get the value in the options

$("#select_element").val()

And try this for get the text in te option selected

$('#slcFoo option:selected').text()

You can see the example here

Re your edit showing:

<option val="x">element1</option>

val is not a valid attribute for option elements. The correct attribute is value, or if you want a custom attribute, use a data- prefix (data-val, for instance).

So depending on whether you meant value or data-val:

If you meant value:

$("#select_element").val() does give you the value (value attribute) of the selected option element in the select box:

console.log($("#select_element").val());
<script src="https://ajax.googleapis./ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<select id="select_element">
  <option value="1">One</option>
  <option value="2" selected>Two</option>
  <option value="3">Three</option>
</select>

If you meant data-val:

If you wanted to get some other attribute from that same option element, you'd do it like this:

var otherAttributeValue = $("#select_element option:selected").attr("data-val");

E.g.:

var otherAttributeValue = $("#select_element option:selected").attr("data-val");
console.log(otherAttributeValue);
<script src="https://ajax.googleapis./ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<select id="select_element">
  <option data-val="first" value="1">One</option>
  <option data-val="second" value="2" selected>Two</option>
  <option data-val="third" value="3">Three</option>
</select>

发布评论

评论列表(0)

  1. 暂无评论