Java Script ฟังค์ชัน
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
// 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<iLen; i++) { opt = options[i]; if (opt.selected) { result.push(opt.value || opt.text); } } return result; } |
วิธีเรียกใช้งาน
1 2 3 4 5 6 7 8 |
<select multiple> <option>opt 1 text <option value="opt 2 value">opt 2 text </select> <button onclick=" var el = document.getElementsByTagName('select')[0]; alert(getSelectValues(el)); ">Show selected values</button> |
0 Comments