首页 > IT职业 > IT职场 > Jquery 操作下拉框
//获取选中项的文本
var testValue = $('#test option[selected]').attr('text');
//获取选中项的值
var testValue = $('#test option[selected]').val();
//获取选中项的文本
var testValue = $('#test option[selected]').text();
//alert(testValue);
//获取选中项的索引
var testValue = $('#test').attr('selectedIndex');
//alert(testValue);
//查找值等于某个的项
var testValue = $("#test option[value='5']").attr('text');
//alert(testValue);
//获取所有的项
var testValue = $("#test option");
testValue.each(function() {
//alert($(this).val());
});
//获取下拉框长度
var testValue = $('#test option').length;
//alert(testValue);
//获取下拉框代码
var testValue = $('#test').html();
//alert(testValue);
//清空下拉框
//$('#test').empty();
//添加单条下拉框选项
$('').appendTo('#test');
$('').appendTo($('#test'));
$("#test").append(""); //添加一项option
$("#test").prepend(""); //在前面插入一项option
//删除单个选项
$('#test option[value="7"]').remove();
$("#test option:last").remove(); //删除索引值最大的Option
$("#test option[index='0']").remove();//删除索引值为0的Option
$("#test option[value='3']").remove(); //删除值为3的Option
$("#test option[text='4']").remove(); //删除TEXT值为4的Option
//设置第二个元素选中
$('#test').attr('selectedIndex', 1);
$('#test')[0].selectedIndex = 3;
$('#test').attr('value', 3);
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/4405/viewspace-2813266/ ,如需转载,请注明出处,否则将追究法律责任。