开发者问题收集

在 javascript 中从 <select> 表单获取值的问题

2012-12-16
129

为什么我能从“Från omgång”列表中获取结果,却不能从“Till Omgång”列表中获取结果?

检查此 此处页面 右侧的“筛选”按钮,然后选择“till omgång”字段中的第二个值,然后会出现错误。

我不知道为什么第二个字段中的值为空,我仔细检查了拼写是否正确。 HTML 代码

<table id="filter-menu-left">
                    <thead>
                        <tr><th id="filterHead" colspan="2"> <h2>Filter</h2></th></tr>
                    </thead>
                <tbody>
                <tr>
                <td>Från omgång</td>
                <td>Till Omgång</td>
                </tr>
                <td>
                <form name='from' method='post' action='formhandler.cgi' id="to">
                <select name='title' onchange='javascript:selectGamesToShowFrom();'>
                      <option value="1">1</option>
                      <option value="2">2</option>
                      <option value="3">3</option>
                      <option value="4">4</option>
                      <option value="5">5</option>
                      <option value="6">6</option>
                      <option value="7">7</option>
                      <option value="8">8</option>
                      <option value="9">9</option>
                      <option value="10">10</option>
                      <option value="11">11</option>
                      <option value="12">12</option>
                      <option value="13">13</option>
                      <option value="14">14</option>
                      <option value="15">15</option>
                      <option value="16">16</option>
                      <option value="17">17</option>
                      <option value="18">18</option>
                      <option value="19">19</option>
                      <option value="20">20</option>
                      <option value="21">21</option>
                      <option value="22">22</option>
                      <option value="23">23</option>
                      <option value="24">24</option>
                      <option value="25">25</option>
                      <option value="26">26</option>
                      <option value="27">27</option>
                      <option value="28">28</option>
                      <option value="29">29</option>
                      <option value="30">30</option>
                    </select>
                    </form>
                </td>
                <td>
                <form name='to' method='post' action='formhandler.cgi'>
                <select name='tit' onchange='javascript:selectGamesToShowTo();'>
                      <option value="1">1</option>
                        <option value="2">2</option>


                    </select>
                    </form>
                </tbody>
                </table>

Java 脚本

方法“selectGamesToShowFrom()”有效,但“to”表单中的值在“selectGamesToShowTo()”方法中无法访问。您能找到错误吗?

function selectGamesToShowFrom(){
    var from = document.from.title.value.toString();//This works
    alert(from + "");

    for (i=0; i<allGamesStringArray.length; i++) {
        var current = parseInt(allGamesStringArray[i].substring(allGamesStringArray[i].length-2,allGamesStringArray[i].length));
        var toCompareWith = parseInt(from);
        if(current<toCompareWith){
            allMarkersOnMap[i].setVisible(false);
            //allMarkersOnMap[i].setMap(null);
        }
        else{
        allMarkersOnMap[i].setVisible(true);
        }
        if(allGamesStringArray[i].indexOf(from)!=-1){
            var t = parseInt(allGamesStringArray[i].substring(allGamesStringArray[i].length-2,allGamesStringArray[i].length));          
        }
    }
}

function selectGamesToShowTo(){

var to = document.to.title.value.toString();//This gives an error that its undefined after I have selected a value in the list and I can't find the error
alert(to);
    for (i=0; i>allGamesStringArray.length; i++) {
        var current = parseInt(allGamesStringArray[i].substring(allGamesStringArray[i].length-2,allGamesStringArray[i].length));
        var toCompareWith = parseInt(from);
        if(current>toCompareWith){
            allMarkersOnMap[i].setVisible(false);
        }
        else{
            allMarkersOnMap[i].setVisible(true);
        }
        if(allGamesStringArray[i].indexOf(from)!=-1){
            alert("hejdar");
            var t = parseInt(allGamesStringArray[i].substring(allGamesStringArray[i].length-2,allGamesStringArray[i].length));          
        }
    }
} 
1个回答
var to = document.to.title.value.toString();

应为

var to = document.to.tit.value.toString();

但是,正如您所指出的,有些浏览器仍然不满意。Opera 不喜欢表单名称为 to ,将其更改为 toForm 并相应地更新代码即可让 Opera 再次满意。

Enrico
2012-12-16