无法从onchange中识别JavaScript函数

时间:2013-05-31 10:25:50

标签: javascript html5

嗨,我收到以下错误:

05-31 11:17:20.427: D/CordovaLog(20889): Uncaught ReferenceError: hello is not defined

在我的html上我已经嵌入以下内容将toggleswitch元素传递给我的javascript函数

 <select name="toggleswitch1" id="toggleswitch1" data-theme="" data-role="slider"
            data-mini="true" onchange="hello(document.getElementById('toggleswitch1'))">

这是我的javascript

<script>
        function hello(toggle)
        {
        var e = toggle;
        var value = e.options[e.selectedIndex].value;
            if value == "yes"
            {
                //do something
            }

        }
  </script>

我错过了什么?

2 个答案:

答案 0 :(得分:3)

您的脚本中有错误:

  <script>
    function hello(toggle)
    {
    var e = toggle;
    var value = e.options[e.selectedIndex].value;
        if (value == "yes")
        {
            //do something
        }

    }
 </script>

答案 1 :(得分:1)

请试试这个:

<script>
    function hello(toggle)
    {
    var e = toggle;
    var value = e.options[e.selectedIndex].value;
        if(value == "yes")
        {
            //do something
        }

    }
</script>

您忘记了if:

上的()

Diff