【问题标题】:Simple JavaScript to access selected value of a select element/dropdown用于访问选择元素/下拉列表的选定值的简单 JavaScript
【发布时间】:2012-11-08 12:27:06
【问题描述】:

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

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 提供的那么多功能,并且使用较小的文件大小来更快地加载页面资源。

那么,我该如何制作可以随心所欲的“$$”对象呢?

【问题讨论】:

  • 试试 HTML DOM,你的工作会轻松很多
  • @Quasarthespacething,请你再解释一下,我没明白你的意思。谢谢。

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


【解决方案1】:
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/

【讨论】:

  • 这样更好(无论如何对于 $$ 函数)。我建议不要使用__defineGetter__ 方法:原型摆弄有点肮脏,它是非标准的,而且它也是deprecated
  • @Martijn 不赞成使用原型摆弄,而是摆弄原型的特定语法。完全可以在原型中添加getter而不使用defineGetter
  • @Asad:你说得对,我没有表达清楚。我建议不要使用__defineGetter__ 方法:它是非标准的且已弃用;我建议不要在任何情况下摆弄原型,因为它有点肮脏的把戏。也就是说,它有时会派上用场。
  • 使用 __defineGetter__ 的原型在 IE9 中不起作用。
  • @Bnikunj 是的,那是因为它是非标准的。使用建议的第一种方法。
【解决方案2】:

谢谢阿萨德。 从您的脚本中,我扩展以涵盖更多功能。这里是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 />

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

【讨论】:

  • 请从您的回答中删除新问题并更新您所犯的错误。否则,你认为它会被删除...
  • 由于我更喜欢​​花括号样式的不同方法,我尝试更改样式,但它在 Firebug 的第 6 行(get text())抛出异常“SyntaxError: missing ; before statement”。您可以找到来源here。问题出在 return 语句上。花括号必须在 return 语句的末尾。更正后的代码是here
猜你喜欢
  • 1970-01-01
  • 2015-03-14
  • 1970-01-01
  • 2013-12-09
  • 1970-01-01
  • 1970-01-01
  • 2012-05-25
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多