简单的JavaScript来访问选择元素/下拉列表的选定值

时间:2012-11-20 12:25:08

标签: javascript jquery select drop-down-menu listbox

我知道如何在下拉列表中获取所选项目的值/文本:

document.getElementById('selNames').options[document.getElementById('selNames').selectedIndex].value

document.getElementById('selNames').options[document.getElementById('selNames').selectedIndex].text

这真是一个很大的代码。所以我创建了一个名为“$$”的函数,它很容易解决这个问题:

function $$(id) { return document.getElementById(id) }

分别使用它来检索值和文本:

$$('selNames').options[$$('selNames').selectedIndex].value
$$('selNames').options[$$('selNames').selectedIndex].text

但我还希望将此代码最小化,如下所示:

$$('selNames').val
$$('selNames').text

我也知道jQuery,但我不想使用它,因为有时我不需要jQuery提供的那么多功能,并且使用较小的文件大小来加快页面资源的加载。

那么,我如何制作可以按我想要的“$$”对象?

2 个答案:

答案 0 :(得分:5)

function $$(id) { 
    var el = document.getElementById(id);
    return {
        element: el,
        get val() {
            return el.options[el.selectedIndex].value;
        },
        get text() {
        return el.options[el.selectedIndex].text;
        }
    };
}

如果你不反对某些原型摆弄,你可以使用它:

HTMLSelectElement.prototype.__defineGetter__("val", function() { return this.options[this.selectedIndex].value; });
HTMLSelectElement.prototype.__defineGetter__("text", function() { return this.options[this.selectedIndex].text; });

以下是演示:http://jsfiddle.net/Ynb8j/

答案 1 :(得分:0)

谢谢阿萨德。 从您的脚本,我扩展到涵盖更多功能。它位于jsfiddle.net及以下:

<script>
function $$(id, optValue) { 
    var el = document.getElementById(id);
    return {
        element: el,
        get text() {
            return el.options[el.selectedIndex].text;
        },
        get value() {
            if(el.type == 'select-one' || el.type == 'select-multiple')
                return el.options[el.selectedIndex].value;
            else
                return el.value;
        },
        get html() {
            return el.innerHTML
        },
        set html(newValue) {
            el.innerHTML = newValue;
        },
        set text(newText) {
            if(optValue == undefined)
                optValue = el.options[el.selectedIndex].value;
            for(i=0; i<el.length; i++)
                if(el.options[i].value == optValue)
                    el.options[i].text = newText;
        },
        set value(newValue) {
            if(el.type == 'select-one' || el.type == 'select-multiple')
            {   if(optValue == undefined)
                    el.options[el.selectedIndex].value = newValue;
                else
                    for(i=0; i<el.length; i++)
                        if(el.options[i].value == optValue)
                            el.options[i].value = newValue;
            }
            else
                el.value = newValue;
        }
    };
}

function f1() { $$('sel1').text = $$('txt1').value }
function f2() { $$('txt2').value = $$('sel1').text }
function f3() { $$('sel2',($$('txt3').value == '' ? undefined : $$('txt3').value)).value = $$('txt4').value }
function f4() { $$('span1').html = $$('txt5').value }
function f5() { $$('txt6').value = $$('span1').html }
</script>

text: <input id='txt1' /> <input type='button' value='set text' onclick='f1()' /> 
<select id="sel1">
    <option value="1">One</option>
    <option value="2">Two</option>
    <option value="3">Three</option>
    <option value="4">Four</option>
    <option value="5">Five</option>
</select>
<input type='button' value='get text' onclick='f2()' /> <input id='txt2' />
<hr />

current value: <input id='txt3' />, new value: <input id='txt4' /> <input type='button' value='set value' onclick='f3()' /> 
<select id="sel2">
    <option value="1">One</option>
    <option value="2">Two</option>
    <option value="3">Three</option>
    <option value="4">Four</option>
    <option value="5">Five</option>
</select>
<hr />

text: <input id='txt5' /> <input type='button' value='set html' onclick='f4()' /> <span id='span1'>span</span>
<input type='button' value='get html' onclick='f5()' /> <input id='txt6' />
<hr />

我稍后会尝试原型方法。我只关心它与旧浏览器的兼容性。