简体   繁体   English

代码中的错误,缺少JS中的基础知识

[英]Error in Code, missing basics in JS

Hi I'll try to keep it short... Last week we started learning Javascript in School. 嗨,我会尽量保持简短......上周我们开始在学校学习Javascript。 Before we learned HTML, CSS and Java(i know it has nothing to do with JS i just want to mention I should know the syntax, from what our teacher told us). 在我们学习HTML,CSS和Java之前(我知道它与JS无关,我只想提一下我应该知道语法,从老师告诉我们的内容)。

So we started with doing Drop-Down-Lists for a form to choose a day, a month and a year. 因此,我们开始为表单选择一天,一个月和一年的下拉列表。 The code then looked like this: 然后代码看起来像这样:

    Day:
    <select name="day">
        <script type="text/javascript">
            var day;

            for(i=1;i<=31;i++){
                day += "<option value='"+i+"'>"+i+"</option>";
            }

            document.write(day);
        </script>
    </select>

This worked. 这很有效。 But our teacher told us one of the nice things about Javascript is to make everything simplier with methods, etc. to avoid rewritting things. 但是我们的老师告诉我们关于Javascript的一个好处是使用方法等简化所有内容以避免重写事物。 We had to do this Drop-Down for a month and a year too, so I thought I would make a function in an external Javascript File. 我们不得不做一个月和一年的Drop-Down,所以我想我会在外部Javascript文件中创建一个函数。

This is the function: 这是功能:

    function date(start, end) {

     var dateV ="";

       for(start==i;i<=end;i++)
       dateV += "<option value='"+i+"'>"+i+"</option>";

    document.write(dateV);
    }

Then i call it in the HTML File: 然后我在HTML文件中调用它:

    Day:
    <select name="day">
        <script type="text/javascript">
            date(1,31);
        </script>
    </select>

But this just displays an empty Drop Down menu. 但这只显示一个空的下拉菜单。 What did i do false? 我做错了什么? I just want to know it to understand the basics of Javascript, because i would really like to learn this script languague. 我只是想知道它来理解Javascript的基础知识,因为我真的很想学习这个脚本。

Thanks for your help. 谢谢你的帮助。

PS: Sorry for my english mistakes, not my main language... PS:对不起我的英语错误,不是我的主要语言......

Check out your date() function: 看看你的date()函数:

function date(start, end) {

    var dateV ="";

    //  vvvvvvvv
    for(start==i;i<=end;i++)
        dateV += "<option value='"+i+"'>"+i+"</option>";

    document.write(dateV);
}

(variable == value) is conditional, and returns true if the variables are equal, and false if they're not. (variable == value)是有条件的,如果变量相等则返回true,如果不相等则返回false。 You want to change start == i to var i = start which means " start the loop by creating a variable i and set it to the value of start " 您想要将start == i更改为var i = start ,这意味着“ 通过创建变量i并将其设置为start的值来start循环

In turn, you'd end up with the following: 反过来,你最终会得到以下结果:

function date(start, end) {

    var dateV ="";

    for(var i = start;i<=end;i++)
        dateV += "<option value='"+i+"'>"+i+"</option>";

    document.write(dateV);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM