JS获取select option获取选中的值多选

2025-04-15 01:39:34
推荐回答(4个)
回答(1):

纯JS

var e = document.getElementById("form-field-select-4");
alert(getSelectValues(e));
// Return an array of the selected opion values
// select is an HTML select element
function getSelectValues(select) {
  var result = [];
  var options = select && select.options;
  var opt;

  for (var i=0, iLen=options.length; i    opt = options[i];

    if (opt.selected) {
      result.push(opt.value || opt.text);
    }
  }
  return result;
}

JQuery

 var selectedValues = [];    
 $("#form-field-select-4 :selected").each(function(){
     selectedValues.push($(this).val()); 
 });
 alert(selectedValues);

回答(2):

  1. 纯JS

  2. var e = document.getElementById("form-field-select-4");
  3. alert(getSelectValues(e));
  4. // Return an array of the selected opion values;
  5. // select is an HTML select element;
  6. function getSelectValues(select) {;
  7. var result = [];
  8. var options = select && select.options;
  9. var opt;
  10. for (var i=0, iLen=options.length; i
  11. opt = options[i];
  12. if (opt.selected) {
  13. result.push(opt.value || opt.text);
  14.     }
  15.   }
  16.   return result;
  17. }

回答(3):

http://www.jb51.net/article/42523.htm

回答(4):

$(#+select标签的id) 不就OK了吗