/获取第一个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’ , KaTeX parse error: Expected 'EOF', got '#' at position 3: ('#̲test option').e…(’#test option’).length - 1).val());
//获取select的长度
$(’#test option’).length;
//添加一个option
$("#test").append(“ff”);
$(“ff”).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();
获取select中选择的text与value相关的值:
获取select选择的Text : var checkText=KaTeX parse error: Expected 'EOF', got '#' at position 3: ("#̲slc1").find("op…("#slc1").val();
获取select选择的索引值: var checkIndex=KaTeX parse error: Expected 'EOF', got '#' at position 3: ("#̲slc1 ").get(0).…("#slc1 option:last").attr(“index”);
添加删除option项:
为select追加一个Option(下拉项) :
$("#slc2").append(""+i+"");
为select插入一个option(第一个位置) :
$("#slc2").prepend(“请选择”); :
PS: prepend 这是向所有匹配元素内部的开始处插入内容的最佳方式。
删除select中索引值最大option(最后一个)
$("#slc2 option:last").remove();
删除select中索引值为0的option(第一个) :
$("#slc2 option[index=‘0’]").remove();
删除select中value='3’的option:
$("#slc2 option[value=‘3’]").remove();
删除select中text='4’的option :
$("#slc2 option[text=‘3’]").remove()
option去重
$(“select option”).each(function() {
text = (this).text();if((this).text();
if((this).text();if((“select option:contains(”+text+")").length > 1)
$(“select option:contains(”+text+"):gt(0)").remove();
});