在文本框中显示if语句的结果

时间:2014-12-12 12:51:21

标签: javascript html if-statement textbox

我有一个我要观看的AZ电影列表,我试图制作一个程序从az中选择一个字母,然后在文本框中显示相应电影的名称,任何人都可以提供帮助,还有一些提示扩大计划以使其更好将是伟大的

<!DOCTYPE html>

<html>
    <head>
        <title>TODO supply a title</title>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <link href="css/stylesheet.css" rel="stylesheet" type="text/css" />

        <script language="javascript" type="text/javascript">
            function randomString() {
                var chars = "1B";
                var string_length = 1;
                var randomstring = '';

                var movie = document.getElementById('movie');

                for (var i = 0; i < string_length; i++) {
                    var rnum = Math.floor(Math.random() * chars.length);
                    randomstring += chars.substring(rnum, rnum + 1);
                }
                document.randform.randomfield.value = randomstring;


                if (i === 1) {
                    movie = "13 Assassins";
                }
                else
                if (i === A) {
                    movie = "Apollo 13";
                }




            }
        </script>

        </head>
        <body>
        <div id="center_page">
            <div id="maincontent">
                <section id="topcontent">
                    <form name="randform">
                        <input type="button" value="Pick a random movie"   onClick="randomString();">&nbsp;
                        <input type="text" name="randomfield" value="">
                        <input type="text" name="movie" id="movie" readonly>

                    </form>

                </section>
            </div>
        </div>
   </body>
</html>

1 个答案:

答案 0 :(得分:0)

尝试......

var movies = [
  "A Movie",
  "B Movie",
  ...
];

var rand = Math.floor(Math.random() * movies.length);
var movieString = movies[rand];
var randomString = movies[rand][1];
document.getElementById('movie').innerHTML = movies[rand];
document.randform.randomfield.value = randomString;

基本上,如果您在上面的数组中有来自AZ的电影列表,此代码将从列表中获得随机电影(无长度),并分配第一个字符(&#34;以&#开头) 34;)用作randomString。电影标题将放入#movie元素(假设文本框)。

此处的工作代码... http://jsbin.com/cogume/2/edit?html,js,output