这篇文章主要介绍了jQuery操作选中select下拉框的值代码实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下js和jQuery联合操作dom真的很好用,如果不是专业前端人员的话,我觉得吧前端语言只要熟练掌握js和jQuery就可以了。
获取select下拉框的几种情况如下:1.获取第一个option的值

$('#test option:first').val();
$('#test option:first').val();2.最后一个option的值

$('#test option:last').val();
$('#test option:last').val();3.获取第二个option的值

$('#test option:eq(1)').val();
$('#test option:eq(1)').val();依次类推可以获取第三个、第四个option的值
4.获取选中的值

var groupid = $("#groupid").find("option:checked").val();
$('#groupidoption:selected').val();
$('#groupid').val();




var groupid = $("#groupid").find("option:checked").val();
$('#groupidoption:selected').val();
$('#groupid').val();



5.设置值为2的option为选中状态

$('#test').attr('value','2');
$('#test').attr('value','2');6.设置最后一个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());
$('#test option:last').attr('selected','selected');
$("#test").attr('value' , $('#test option:last').val());
$("#test").attr('value' , $('#test option').eq($('#test option').length - 1).val());7.获取select的长度

$('#test option').length;
$('#test option').length;8.添加一个option

var str="";
$("#test").append(str); //一般都用这个追加

$("").appendTo("#test");
var str="";
$("#test").append(str); //一般都用这个追加

$("").appendTo("#test");9.删除选中项

$('#test option:selected').remove();
$('#test option:selected').remove();10.删除项选中的第一项

$('#test option:first').remove();
$('#test option:first').remove();11.删除满足条件的option
$('#test option').each(function(){

if( $(this).val() == '5'){

$(this).remove();

}
});

$('#test option[value=5]').remove();
$('#test option').each(function(){

if( $(this).val() == '5'){

$(this).remove();

}
});

$('#test option[value=5]').remove();以上就是本文的全部内容,希望对大家的学习有所帮助。