Leif160519的blog Leif160519的blog

——————

目录
js 获取html的select中的option的值
/    

js 获取html的select中的option的值

html代码:

<!DOCTYPE html>  
<html>  
<body>  
  
<select id="testSelect">  
  <option value="volvo">Volvo</option>  
  <option value="saab">Saab</option>  
  <option value="opel">Opel</option>  
  <option value="audi">Audi</option>  
</select>  
    
</body>  
</html>  

获取方法:

var obj = document.getElementByIdx_x(”testSelect”); //定位id
var index = obj.selectedIndex; // 选中索引
var text = obj.options[index].text; // 选中文本
var value = obj.options[index].value; // 选中值
$('#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;//索引

更简单的:

var value = document.getElementById("testSelect").value; 

“The first 90% of the code accounts for the first 90% of the development time. The remaining 10% of the code accounts for the other 90% of the development time.” – Tom Cargill

标  题 js 获取html的select中的option的值
作  者Leif160519
出  处https://github.icu/articles/2019/09/04/1567563034055.html
关于博主:坐标南京,运维工程师,如有问题探讨可以直接下方留言。
声援博主:如果您觉得文章对您有帮助,可以评论、订阅、收藏。您的鼓励是博主的最大动力!