Home  >  Article  >  Web Front-end  >  js code to get the current select element value_form effects

js code to get the current select element value_form effects

WBOY
WBOYOriginal
2016-05-16 18:29:09915browse
  1. If all option elements under the select element do not specify the selected attribute, the first one will be selected by default.
  2. The index of the selected option element can be obtained through select.selectedIndex.
  3. The selected option element can be obtained through select.options[select.selectedIndex].
  4. option element, the value attribute value of the option element can be obtained through option.value, which is value3; the option element can be obtained through option.text The text inside is text3.
  5. If the option element does not define the value attribute, option.value cannot be obtained in IE, but Safari, Opera, and FireFox can still obtain it through option.value, and the value is the same as option.text.
  6. You can use option.attributes.value && option.attributes.value.specified to determine whether the option element defines the value attribute.

Therefore, the script to obtain the current select element value is as follows:

Copy the code The code is as follows:

var getSelectValue = funtion(select) {
var idx = select.selectedIndex,
option,
value;
if (idx > -1) {
option = select.options [idx];
value = option.attributes.value;
return (value && value.specified) ? option.value : option.text);
}
return null;
}
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn