Jquery操作select

    技术2022-05-19  21

    Javascript代码

    添加“江苏”到下拉框的最后一位

    1$('#add_to_last').click(function(){ 2    $('#select').append(' 3<OPTION value="江苏"></OPTION> 4   5江苏 6   7'); 8});

    添加“安徽”到下拉框的第一位

    Javascript代码

    1$('#add_to_first').click(function(){ 2    $('#select').prepend(' 3<OPTION value="安徽"></OPTION> 4   5安徽 6   7'); 8});

    获取当前的selectIndex(当前选中的下拉菜单的项目的索引)

    Javascript代码

    1$('#get_select_index').click(function(){ 2    alert($('#select option:selected').attr("index")); 3});

    移除下拉菜单最后一个项目

    Javascript代码

    1$('#remove_last_option').click(function(){ 2    $('#select option:last').remove(); 3});

    移除除了第一个之外的所有选项

    1$('#remove_all_option_except_first').click(function(){ 2    $('#select option').not(':first').remove(); 3});

    获取下拉菜单最大索引值

    1$('#get_max_index').click(function(){ 2    var maxIndex=$("#select option:last").attr("index"); 3    alert(maxIndex); 4});

    =====================================================================

    //获取第一个option的值   $('#test option:first').val();   //最后一个option的值    $('#test option:last').val();   //获取第二个option的值   $('#test option:eq(1)').val();   //获取选中的值   $('#test').val();   $('#test option:selected').val();   //设置值为2的option为选中状态   $('#test').attr('value','2');   //设置第一个option为选中   $('#test option:last').attr('selected','selected');   $("#test").attr('value' , $('#test option:last').val());   $("#test").attr('value' , $('#test option').eq($('#test option').length - 1).val());   //获取select的长度   $('#test option').length;   //添加一个option   $("#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();      //获取第一个Group的标签   $('#test optgroup:eq(0)').attr('label');   //获取第二group下面第一个option的值    $('#test optgroup:eq(1) :option:eq(0)').val();  

     


    最新回复(0)