开发者

JQuery option value sometimes undefined

开发者 https://www.devze.com 2023-01-30 09:40 出处:网络
I am having trouble with a simple select-option. Most of the time the selected-value (in the action var) yields the correct result (\"closed\") but sometimes it is \"undefined\".

I am having trouble with a simple select-option. Most of the time the selected-value (in the action var) yields the correct result ("closed") but sometimes it is "undefined".

$("#doaction").click(function () {
  // action sometimes is "undefined" =>
  var action = $("#workflow option:selected").val();        
}

<select name="workflow" id="workflow">
  <option value="-1">initial state</option>
  <option value="close">perform actio开发者_运维百科n close</option>
</select>

<a href="#" id="doaction">submit</a>

Any hints are welcome.


Why not just:

$("#workflow").val()

The val() method will return the current selected option of a select element.


Using

$("#workflow option:selected").val()

is equivalent to

$("#workflow").val()


You should do

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

See documentation

0

精彩评论

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