ITPub博客

首页 > IT职业 > IT职场 > Jquery 操作下拉框

Jquery 操作下拉框

IT职场 作者:tong_sun 时间:2021-09-09 09:50:48 0 删除 编辑

 

//获取选中项的文本

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/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2019-08-12

  • 博文量
    89
  • 访问量
    793