Home  >  Article  >  Web Front-end  >  JS获取select-option-text_value的方法_javascript技巧

JS获取select-option-text_value的方法_javascript技巧

WBOY
WBOYOriginal
2016-05-16 17:06:551116browse

HTML代码:

复制代码 代码如下:


用jQuery获取 select选中对象的value、text

复制代码 代码如下:

$(function(){
    $("#month").change(function() {
        alert($(this).find("option:selected").val());
        alert($(this).find("option:selected").text());
    });
});

如果用JS获取 select选中对象的value是很容易的

复制代码 代码如下:

function selectInput(oSelect) {
    alert(oSelect.value);
}

如果要用JS获取 select选中对象的text呢 ?

复制代码 代码如下:

function selectInput(oSelect) {
    alert(oSelect.options[oSelect.selectedIndex].text);
}

如果select 没有默认则设第一个为默认?

复制代码 代码如下:

document.getElementById("test“).options[0].selected=true;
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn