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

javascript - Val of Select = null, if values is disabled - Stack Overflow

programmeradmin3浏览0评论

I have Select and all values is disabled, how can I set the value to this Select?

<select id="testselect">
   <option disabled>1</option>
   <option disabled>2</option>
   <option disabled selected="selected">3</option>
</select>

alert($("#testselect").val()); //result null

jsfiddle

I have Select and all values is disabled, how can I set the value to this Select?

<select id="testselect">
   <option disabled>1</option>
   <option disabled>2</option>
   <option disabled selected="selected">3</option>
</select>

alert($("#testselect").val()); //result null

jsfiddle

Share asked Apr 26, 2017 at 11:22 AlexAlex 2,7754 gold badges31 silver badges45 bronze badges 1
  • 4 use $("#testselect option:selected") look here jsfiddle/arjqk00f – Carsten Løvbo Andersen Commented Apr 26, 2017 at 11:23
Add a ment  | 

2 Answers 2

Reset to default 4

Try This ==>

alert($('#testselect option[disabled]:selected').val());
<script src="https://code.jquery./jquery-2.2.4.min.js"></script>
<select id="testselect">
  <option disabled>1</option>
  <option disabled>2</option>
  <option disabled selected>3</option>
</select>

You have to be specific about value of which element to return.

alert($("#testselect option:selected").val());
<script src="https://ajax.googleapis./ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<select id="testselect">
  <option disabled>1</option>
  <option disabled>2</option>
  <option disabled selected>3</option>
</select>

发布评论

评论列表(0)

  1. 暂无评论