Javascript代码
添加“江苏”到下拉框的最后一位
1 | $( '#add_to_last' ).click( function (){ |
3 | <OPTION value= "江苏" ></OPTION> |
添加“安徽”到下拉框的第一位
Javascript代码
1 | $( '#add_to_first' ).click( function (){ |
3 | <OPTION value= "安徽" ></OPTION> |
获取当前的selectIndex(当前选中的下拉菜单的项目的索引)
Javascript代码
1 | $( '#get_select_index' ).click( function (){ |
2 | alert($( '#select option:selected' ).attr( "index" )); |
移除下拉菜单最后一个项目
Javascript代码
1 | $( '#remove_last_option' ).click( function (){ |
2 | $( '#select option:last' ).remove(); |
移除除了第一个之外的所有选项
1 | $( '#remove_all_option_except_first' ).click( function (){ |
2 | $( '#select option' ).not( ':first' ).remove(); |
获取下拉菜单最大索引值
1 | $( '#get_max_index' ).click( function (){ |
2 | var maxIndex=$( "#select option:last" ).attr( "index" ); |
=====================================================================
-
- $('#test option:first').val();
-
- $('#test option:last').val();
-
- $('#test option:eq(1)').val();
-
- $('#test').val();
- $('#test option:selected').val();
-
- $('#test').attr('value','2');
-
- $('#test option:last').attr('selected','selected');
- $("#test").attr('value' , $('#test option:last').val());
- $("#test").attr('value' , $('#test option').eq($('#test option').length - 1).val());
-
- $('#test option').length;
-
- $("#test").append("<option value='9'>ff</option>");
- $("<option value='9'>ff</option>").appendTo("#test");
-
- $('#test option:selected').remove();
-
- $('#test option:first').remove();
-
- $('#test option').each(function(){
- if( $(this).val() == '5'){
- $(this).remove();
- }
- });
- $('#test option[value=5]').remove();
-
-
- $('#test optgroup:eq(0)').attr('label');
-
- $('#test optgroup:eq(1) :option:eq(0)').val();