中国程序员网吧 关注:1贴子:12
  • 2回复贴,共1

基于jquery的select各种操作详解

只看楼主收藏回复

1. 获取Select
获取select 选中的 text :
$("#selectname").find("option:selected").text();获取select选中的 value:
$("#selectname").val();获取select选中的索引:$("#selectname").get(0).selectedIndex;2. 设置select
设置select 选中的索引:
$("#selectname ").get(0).selectedIndex=index;//index为索引值设置select 选中的value:
$("#selectname").attr("value","Normal“);$("#selectname").val("Normal");$("#selectname").get(0).value = value;设置select 选中的text:
var count=$("#selectname option").length; for(var i=0;i<count;i++) { if($("#selectname").get(0).options[i].text == text) { $("#selectname").get(0).options[i].selected = true; break; } } $("#select_id option[text='jQuery']").attr("selected", true);设置select option项:
$("#select_id").append("<option value='Value'>Text</option>"); //添加一项option$("#select_id").prepend("<option value='0'>请选择</option>"); //在前面插入一项option$("#select_id option:last").remove(); //删除索引值最大的Option$("#select_id option[index='0']").remove();//删除索引值为0的Option$("#select_id option[value='3']").remove(); //删除值为3的Option$("#select_id option[text='4']").remove(); //删除TEXT值为4的Option


IP属地:浙江1楼2016-09-09 13:53回复
    本文更多内容请点击地址:http://u.cxyblog.com/28/article-aid-3491.html
    也可以访问官网: http://www.cxyblog.com
    欢迎加入前端qq交流群:457240118


    IP属地:浙江2楼2016-09-09 13:54
    回复
      详细内容请点击:http://u.cxyblog.com/28/article-aid-3491.html


      IP属地:浙江5楼2016-09-13 11:25
      回复