jQuery操作SelectのOption上下移動や追加削除など

3523 ワード

 
  




<br>/** <br>* option <br>*/ <br>function upSelectedOption(){ <br>if(null == $('#where').val()){ <br>alert(' '); <br>return false; <br>} <br>// , 0 <br>var optionIndex = $('#where').get(0).selectedIndex; <br>// , <br>if(optionIndex > 0){ <br>$('#where option:selected').insertBefore($('#where option:selected').prev('option')); <br>} <br>} <br> <br>/** <br>* option <br>*/ <br>function downSelectedOption(){ <br>if(null == $('#where').val()){ <br>alert(' '); <br>return false; <br>} <br>// , 1 <br>var optionLength = $('#where')[0].options.length; <br>// , 0 <br>var optionIndex = $('#where').get(0).selectedIndex; <br>// , <br>if(optionIndex < (optionLength-1)){ <br>$('#where option:selected').insertAfter($('#where option:selected').next('option')); <br>} <br>} <br> <br>/** <br>* option <br>*/ <br>function removeSelectedOption(){ <br>if(null == $('#where').val()){ <br>alert(' '); <br>return false; <br>} <br>$('#where option:selected').remove(); <br>} <br> <br>/** <br>* option <br>*/ <br>function getSelectedOption(){ <br>// Select Text <br>var checkText = $('#where').find('option:selected').text(); <br>// Select Value <br>var checkValue = $('#where').val(); <br>alert(' text=' + checkText + ', value=' + checkValue); <br>var ids = ''; <br>var options = $('#where')[0].options; <br>for(var i=0; i<options.length; i++){ <br>ids = ids + '`' + options[i].id; <br>} <br>alert(' ' + ids); <br>} <br> <br>/** <br>* option <br>*/ <br>function addSelectedOption(){ <br>// <br>$('#where').prepend('<option value="hbin" id="where06">Haerbin</option>'); <br>// <br>$('#where').append('<option value="hlj" id="where07">HeiLongJiang</option>'); <br>$('#where').attr('size', 7); <br>} <br>