jQuery获取各种标签的文本和value值

时间:2019-09-14
本文章向大家介绍jQuery获取各种标签的文本和value值,主要包括jQuery获取各种标签的文本和value值使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。
<select id="test">
  <option value ="volvo">Volvo</option>
  <option value ="saab">Saab</option>
  <option value="opel">Opel</option>
  <option value="audi">Audi</option>
</select>
 
获取select 选中的 text :
    $("#test").find("option:selected").text();
 
获取select选中的 value:
    $("#test").val();
 
获取select选中的索引:
    $("#test").get(0).selectedindex;
 
设置select:
设置select 选中的索引:
    $("#test").get(0).selectedindex=index;//index为索引值
 
设置select 选中的value:
    $("#test").attr("value","normal“);
    $("#test").val("normal");
    $("#test").get(0).value = value;
 
设置select option项:
 
    $("#test").append("<option value='value'>text</option>");  //添加一项option
    $("#test").prepend("<option value='0'>请选择</option>"); //在前面插入一项option
    $("#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
 
 
清空 select:
 
    $("test").empty();

原文地址:https://www.cnblogs.com/sea-stream/p/11519853.html