Google Chrome上的selectedIndex-javascript

Google Chrome上的selectedIndex-javascript,javascript,jquery,Javascript,Jquery,有谁能告诉我为什么谷歌Chrome上没有这个功能: val = document.form.option[document.form.selectedIndex].value; 我应该如何避免这种情况,以免其他浏览器(如IE和FF)出错。 非常感谢。试试这个: <select id="option"> .... </select> 试试这个: <select id="option"> .... </select> 这是因为您实际上并没有选择表

有谁能告诉我为什么谷歌Chrome上没有这个功能:

val = document.form.option[document.form.selectedIndex].value;
我应该如何避免这种情况,以免其他浏览器(如IE和FF)出错。
非常感谢。

试试这个:

<select id="option">
....
</select>
试试这个:

<select id="option">
....
</select>

这是因为您实际上并没有选择表单元素。应该是这样的(我假装您的选择名为
myselect
):


这是一个。

因为您实际上并没有选择表单元素。应该是这样的(我假装您的选择名为
myselect
):

这是一个。

$('select.foo').val();//从下拉列表中获取值选择更容易

jQuery(1.4.2)当元素为
选择时,函数
val
的代码

if ( jQuery.nodeName( elem, "select" ) ) {
    var index = elem.selectedIndex,
        values = [],
        options = elem.options,
        one = elem.type === "select-one";

    // Nothing was selected
    if ( index < 0 ) {
        return null;
    }

    // Loop through all the selected options
    for ( var i = one ? index : 0, max = one ? index + 1 : options.length; i < max; i++ ) {
        var option = options[ i ];

        if ( option.selected ) {
            // Get the specifc value for the option
            value = jQuery(option).val();

            // We don't need an array for one selects
            if ( one ) {
                return value;
            }

            // Multi-Selects return an array
            values.push( value );
        }
    }

    return values;
}
if(jQuery.nodeName(elem,“select”)){
var指数=elem.selectedIndex,
值=[],
选项=元素选项,
一个=元素类型==“选择一个”;
//未选择任何内容
如果(指数<0){
返回null;
}
//循环浏览所有选定的选项
对于(var i=one?索引:0,max=one?索引+1:options.length;i

$('select.foo').val();//从下拉列表中获取值选择更容易

jQuery(1.4.2)当元素为
选择时,函数
val
的代码

if ( jQuery.nodeName( elem, "select" ) ) {
    var index = elem.selectedIndex,
        values = [],
        options = elem.options,
        one = elem.type === "select-one";

    // Nothing was selected
    if ( index < 0 ) {
        return null;
    }

    // Loop through all the selected options
    for ( var i = one ? index : 0, max = one ? index + 1 : options.length; i < max; i++ ) {
        var option = options[ i ];

        if ( option.selected ) {
            // Get the specifc value for the option
            value = jQuery(option).val();

            // We don't need an array for one selects
            if ( one ) {
                return value;
            }

            // Multi-Selects return an array
            values.push( value );
        }
    }

    return values;
}
if(jQuery.nodeName(elem,“select”)){
var指数=elem.selectedIndex,
值=[],
选项=元素选项,
一个=元素类型==“选择一个”;
//未选择任何内容
如果(指数<0){
返回null;
}
//循环浏览所有选定的选项
对于(var i=one?索引:0,max=one?索引+1:options.length;i
你到底想做什么?如果您试图获取select元素的值,请使用jQuery的
.val()
。您到底想做什么?如果试图获取select元素的值,请使用jQuery的
.val()