js获取select选中的内容

时间:2023-03-09 09:18:57
js获取select选中的内容

获取select选中的内容


  1. js获取select标签选中的值
var obj = document.getElementById("selectId");//获取select对象
var index = obj.selectedIndex; // 选中索引
var text = obj.options[index].text; // 选中文本
var value = obj.options[index].value; // 选中值
  1. jQuery中获得选中select值
  • 第一种方法:
$('#testSelect option:selected').text();//选中的文本
$('#testSelect option:selected') .val();//选中的值
$("#testSelect ").get(0).selectedIndex;//索引
  • 第二种方式
$("#tesetSelect").find("option:selected").text();//选中的文本
$("#tesetSelect").find("option:selected").val();
$("#tesetSelect").find("option:selected").get(0).selectedIndex;