【问题标题】:Uncaught TypeError: Cannot read property 'undefined' of undefined未捕获的类型错误:无法读取未定义的属性“未定义”
【发布时间】:2015-01-06 16:29:35
【问题描述】:

我有选择标签的问题。我用谷歌搜索并没有找到关于我的问题的任何解决方案。这是链接之一...(Uncaught TypeError: Cannot read property 'value' of undefined).....我在选择标签中的onchange事件上有一些功能,就像这样......

           <select id="selectBox" onchange="_change();">
                    <?php
                    $i = 1;
                    while ($lastpage >= $i):?>
                    <option><?php echo $i; ?></option>
                        <?php
                        $i++;
                    endwhile;
                    ?>
          </select>

可能的 jQuery 代码 ...

   <script type="text/javascript">
     function _change(evt)
        {
            alert(this.options[this.selectedIndex].value);
        };
    </script>

问题是为什么我得到未定义的未定义而不是未定义的值或属性,以便我可以继续解决该问题... 感谢任何帮助将不胜感激...

【问题讨论】:

  • 第一步是从你的服务器端代码中删除多余的&lt;/a&gt;
  • 对不起,它没有出现在代码中但被错误地添加到这里

标签: javascript typeerror


【解决方案1】:

您必须将referrer 元素this 传递给您的javascript 函数。

代码:

<select id="selectBox" onchange="_change(this);">
    <option value="1">1</option>
    <option value="2">2</option>
    <option value="3">3</option>
</select>

js:

function _change(el) {
   alert(el.options[el.selectedIndex].value);
};

演示:http://jsfiddle.net/g5hzph1y/

【讨论】:

    【解决方案2】:
    ***SIMPLEST REPRESENTATION:-***    
    
    <!DOCTYPE html>
        <html>
        <body>
    
        Select a fruit and click the button:
        <select id="mySelect">
          <option value="11">Apple</option>
          <option value="12">Orange</option>
          <option value="13">Pineapple</option>
          <option value="14">Banana</option>
        </select>
    
        <button type="button" onclick="myFunction()">Display index</button>
    
        <script>
        function myFunction() {
            var x = document.getElementById("mySelect").selectedIndex;
            var y = document.getElementById("mySelect").options;
            alert("Index: " + y[x].index + " text is " + y[x].text + " and value is " +  y[x].value);
        }
        </script>
    
        </body>
        </html>
    

    【讨论】:

      猜你喜欢
      • 2017-07-26
      • 2021-12-22
      • 1970-01-01
      • 2019-02-26
      • 2016-04-28
      • 2021-05-10
      • 2019-07-31
      相关资源
      最近更新 更多