【问题标题】:how to compare and call function in javascript如何在javascript中比较和调用函数
【发布时间】:2016-01-27 23:38:10
【问题描述】:

我正在学习 javascript,我正在制作一个表单,它将要求输入起始地址和目标地址。我想要做的 - 但无法弄清楚 - 如果我的输入框并且如果它存在于函数中它将显示一条消息,但如果它不存在它仍然会循环直到它找到值并显示消息或显示错误(如果它真的没有任何东西)。

基于此 HTML 表单:

<div id="panel">
<form>
<input type="text" placeholder="Start Address" id="start" >

<input type="text" placeholder="Destination Address" id="end">

<input type="button" value="Get Direction"  id="SubmitBtn" onclick="printme()" >


</form>
</div>
<div id="infopanel">
</div>


    function printme(){

       var start = document.getElementById("start").value;
   var end = document.getElementById("end").value;
   var  point1="point1";
       var  point2="point2";
       var  point3"point3";

if (start==point1&&end==point2){
    document.getElementById("infopanel").innerHTML="point1 to point2 just ride a taxi";
    }
else if (start==point1&&end==point3){
    document.getElementById("infopanel").innerHTML="point1 to point3 just ride a bus";
}
    else document.getElementById("infopanel").innerHTML="no route found we will update    you       ";
}

示例:如果 start 等于 point1 并且 end 等于 point2 则显示“只是乘坐出租车”否则如果 start 等于 point 1 并且结束是 point3 则显示“只是乘坐公共汽车”否则显示“找不到路线我们将更新你“

【问题讨论】:

  • Sooooooo... 你试过什么?你的 Javascript 在哪里?

标签: javascript html


【解决方案1】:
function printme() {
    var start = document.getElementById('start').value;
    var end = document.getElementById('end').value;

    if (start !== end) {
        alert("can't go there!");
    } else {
        alert("you must ride a " + start.split(' ')[0]); // appends the first word entered into the input
    }
}

【讨论】:

  • 但是如果 start 的值是马来西亚而 end 是澳大利亚而不是你必须骑一个平原,它会说“不能去那里因为(开始!==结束)。对吧?
  • 不是一个应用程序,但可以找到在不同地点之间旅行以用于教育目的的方式 :) 感谢您的提议
  • 我只是在讽刺 :) (执行您想要的操作所需的代码可能比单个 SO 答案大)。不过,如果您对其进行编码并稍后有任何具体问题,我将非常乐意提供帮助
猜你喜欢
  • 1970-01-01
  • 2016-09-06
  • 1970-01-01
  • 2010-10-01
  • 2011-09-27
  • 2022-11-16
  • 1970-01-01
  • 1970-01-01
  • 2014-05-24
相关资源
最近更新 更多