<span>编程语言:</span>
<select id="test" name="selectItem">
<option value="1">Java</option> <option value="2">PHP</option>
<option value="3">Android</option>
</select>
JQuery方法获取:
var language= $( '#test option:selected').val();
console.info(language);//例如选中Java,将输出:1
var languageText= $( '#test option:selected').text();
console.info(languageText);//例如选中PHP,将输出:PHP
JavaScript方法获取:
1:拿到select对象: var myselect=document.getElementById("test");
2:拿到选中项的索引:var index=myselect.selectedIndex ; // selectedIndex代表的是你所选中项的index
3:拿到选中项options的value: myselect.options[index].value;
4:拿到选中项options的text: myselect.options[index].text;
本文介绍如何使用JavaScript和JQuery从下拉框中获取被选中的选项的值和文本。通过具体示例展示了两种不同方法的操作步骤。
4239

被折叠的 条评论
为什么被折叠?



