Javascript函数说没声明?

时间:2014-12-20 10:41:11

标签: javascript html5 function

写一些相当基本的html ..当我尝试点击按钮没有任何反应..但是我的控制台中记录了一个错误,因为没有定义说英语

<head>
    <meta charset="utf-8">
    <title>Language Translator</title>

    <script type='text/javascript'>
        function speakenglish() {
            var ddl = document.getElementById("ispeakenglish");
            var selectedValue = ddl.options[ddl.selectedIndex].value;
                if (selectedValue === "selectlanguage") {
                alert("Please select a answer");
                }
                if (selectedValue === "speakenglishno") {
                alert("Sorry to partake in our service you will need to be able to speak english");
                }
                if (selectedValue === "speakenglishyes")
                { 
                    location.replace({% static '/page2.html' %});
                }
     }
    </script>
</head>

<body>
    <h1> Basic Language Translator</h1>
    <p> A Userbase generated language translator </p>
    <label class="languespoken" for="ispeakenglish">Do you know English?</label>
    <select id="cardtype" name="cards">
        <option value="selectlanguage">--- Please select ---</option>
        <option value="speakenglishyes">Yes</option>
        <option value="speakenglishno">No</option>
    </select>

    <input type="button" onclick="speakenglish()" value="Proceed">

</body>

5 个答案:

答案 0 :(得分:3)

更改id

 var ddl = document.getElementById("cardtype");

因为您在该页面中没有id ispeakenglish

您也能解释一下这段代码的作用location.replace({% static '/page2.html' %});吗?

答案 1 :(得分:1)

这是因为您没有ispeakenglish id的任何元素。所以函数有错误。

TypeError: ddl is null

快速解决方案是替换

var ddl = document.getElementById("ispeakenglish");

var ddl = document.getElementById("cardtype");

答案 2 :(得分:1)

在这一行

 location.replace({% static '/page2.html' %});

您遇到语法错误。因此,无法解析<script>标记,并且函数仍未定义。

我不确定,你想在那里做什么,所以我无法解决它。如果这只是一个重定向,你可能想要使用:

 window.location = '/page2.html';

答案 3 :(得分:1)

您的函数声明中存在语法错误:

SyntaxError: invalid property id
location.replace({% static '/page2.html' %});

修复它,你的功能将正常工作:)

答案 4 :(得分:1)

试试这个:

 <head>
<meta charset="utf-8">
<title>Language Translator</title>

<script type="text/javascript">
    function speakenglish() {
        var ddl = document.getElementById("cardtype");
        var selectedValue = ddl.options[ddl.selectedIndex].value;
            if (selectedValue === "selectlanguage") {
            alert("Please select a answer");
            }

            if (selectedValue === "speakenglishno") {
            alert("Sorry to partake in our service you will need to be able to speak english");
            }
            if (selectedValue === "speakenglishyes")
            { 
                //location.replace({% static '/page2.html' %}); //change It
                window.location = 'page2.html';
            }
 }
</script>
</head>

<body>
<h1> Basic Language Translator</h1>
<p> A Userbase generated language translator </p>
<label class="languespoken" for="ispeakenglish">Do you know English?</label>
<select id="cardtype" name="cards">
    <option value="selectlanguage">--- Please select ---</option>
    <option value="speakenglishyes">Yes</option>
    <option value="speakenglishno">No</option>
</select>

<input type="button" onclick="speakenglish()" value="Proceed">
</body>