Home > Web Front-end > JS Tutorial > body text

JS method to get select-option-text_value_javascript skills

WBOY
Release: 2016-05-16 17:06:55
Original
1178 people have browsed it

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;
Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template