Javascript帮助(If和If / Else Statemtents)? [英] Javascript Help (If and If/Else Statemtents)?

查看:115
本文介绍了Javascript帮助(If和If / Else Statemtents)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在开始之前,我是编码的初学者,所以我会接受任何批评/建议。



我正在尝试创建一个代码将一个引脚放在地球上的给定位置。我已经知道如何将标记放在地球上。我只需要弄清楚如何创建一个文本框,用户可以在其中键入一个位置,它将添加一个图钉。出于某种原因,无论我在文本框中输入什么内容,程序都会在第一个位置放置一个引脚:Adelaide。如果你能帮我弄清楚我做错了什么,我将不胜感激。



这是代码:



Before I begin, I am a beginner at coding, so I will take any and all critism/suggestions.

I'm trying to create a code that places a pin on a given location on the globe. I already know how to place the marker on the globe. I just need to figure out how to create a textbox where users can type in a location and it will add a pin. For some reason, no matter what I type into the textbox, the program places a pin on the first location: Adelaide. If you could help me figure out what I'm doing wrong, it would be greatly appreciated.

Here is the code:

<br><center><p><font face = "forte"><font size="6"><font color="cyan">Add a Pin</font></p>
<input type="text" id="textinput">
<input type="button" value="Add" onclick="addPin()"><br>

<script type="text/javascript">
    function addPin()
    {
        var textinput = document.getElementById("textinput").value;
        if (textinput=="Adelaide" || "Adelaide, Australia" || "Adelaide Australia" || "Adelaide, South Australia, Australia")
            {
            document.getElementById("disptext").innerHTML = ("<font color='cyan'><font size='3'>Pin addded onto Adelaide, South Australia, Australia!</font>");
            var marker = WE.marker([-34.55, 138.36]).addTo(earth);
            marker.bindPopup("<b>Adelaide</b><br>Australia", {maxWidth: 120, closeButton: true});
            }
        else if (textinput=="Amsterdam" || "Amsterdam, Netherlands" || "Amsterdam Netherlands" || "Amsterdam, the Netherlands" || "Amsterdam the Netherlands")
            {
            document.getElementById("disptext").innerHTML = ("<font color='cyan'><font size='3'>Pin addded onto Amsterdam, Netherlands!</font>");
            var marker = WE.marker([52.22, 4.53]).addTo(earth);
            marker.bindPopup("<b>Amsterdam</b><br>Netherlands", {maxWidth: 120, closeButton: true});
            }
        else
            {
            document.getElementById("disptext").innerHTML = "<font color='red'><font size='3'>Sorry, there was an error.</font>"}
    }
</script>

<p id="disptext"></p>

</head>

             <script src="http://www.webglearth.com/v2/api.js"></script>
       <script>
          var earth;
          function initialize() {
          earth = new WE.map('earth_div');
          earth.setView([20, 3], 3);
          WE.tileLayer('http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png',{
          }).addTo(earth);


          var before = null;
          requestAnimationFrame(function animate(now) {
            var c = earth.getPosition();
            var elapsed = before? now - before: 0;
            before = now;
            earth.setCenter([c[0], c[1] + 0.1*(elapsed/15)]);
            requestAnimationFrame(animate);
          });
          }
        </script>
        <style>
        html, body{padding: 0; margin: 0;}
        #earth_div{top: 0; right: 0; bottom: 0; left: 0;}
        </style>
        </head>
        <body onload="initialize()">
        <div id="earth_div"></div>
    </div>

推荐答案

因为那不是,如果...... else 块有效。 if else块(带有复杂表达式)要求您使用AND运算符(您是)以及解析为布尔值的不同表达式或变量或语句( true )。您的代码不符合指南。



我将采取第一个条件块,并解释那里出了什么问题。



Because that is not how if...else block works. An if else block (with complex expressions) requires you to use AND operator (which you are) along with different expressions or variable or statements that resolve to boolean value (true or false). Your code does not follow the guidelines.

I will take the first conditional block, and explain what went wrong there.

if (textinput=="Adelaide" || "Adelaide, Australia" || "Adelaide Australia" || "Adelaide, South Australia, Australia")





现在在上面的代码块中,您只使用一个表达式来确定值。其余的表达式(文字)只是文字,但是OP运算符,常量文字和这个表达式......它们使你的第一个表达式为 true 始终 [ ^ ]。



对此的解决方案是将文字更改为相同的表达式以检查它们是否为值。





Now in the above code block, you are using only one expression to determine the value. Rest of the expressions (literals) are just literals, however the OP operators, constant literals and this expression... They make your first expression to be true, always[^].

A solution to this is that you change your literals into same expression to check whether they are the values or not.

if (textinput=="Adelaide" || // One 
    textinput=="Adelaide, Australia" || // condition
    textinput=="Adelaide Australia" || // at a 
    textinput=="Adelaide, South Australia, Australia") // time!





现在,如果您要运行此代码,您会发现代码确实有效。如果匹配,则会显示结果;真正。否则它将被解析为假,代码将转移到下一阶段;假。
转到此JSFiddle以查看此实例 [ ^ ]。



听着,我们不是来批评,但是要帮助。我们的答案取决于您的问题;问一个好问题得到一个好答案,问一个愚蠢的问题得到一个愚蠢的回应


这篇关于Javascript帮助(If和If / Else Statemtents)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆