简体   繁体   English

如何获取用户的输入并返回已定义的匹配变量的值?

[英]How do I take a user's input and return the value of a matching variable I have defined?

  1. User puts in three letter code 用户输入三个字母代码
  2. Function finds a variable with the same three letter code 函数查找具有相同三个字母代码的变量
  3. If the number assigned to this variable is > 10200, alert "YES" 如果分配给该变量的数字> 10200,则警告“是”

 var form = document.getElementById("airportForm") var input = document.getElementById("airport") //user input from form var airport = { lax : 12091, jfk : 14511, ewr : 11000, lga : 7003, phl : 10506, dca : 7169, iad : 11500, bos : 10083, btv : 8320, mht : 9250, pwm : 7200, npt : 2999, hpn : 6549, bdl : 9510, bwi : 10502, pit : 11500, ord: 13001, mdw : 6522, ind : 11200, lax : 12091, sfo : 11870, san : 9401, las : 14510, sea : 11901, dfw : 13401, aus : 12248, atl : 11890, pbi : 10008, mco : 12005, lhr : 12799, lgw : 10364, man : 10000, lcy : 4948, dub : 8652, snn : 10495, cdg : 13829, ory : 11975, mad : 14272, bcn : 11654, opo : 11417, lis : 12484, dxb : 13123, auh : 13451, dwc : 14764, jed : 12467, ruh : 13796, kwi : 11483, ebl : 13891, sda : 13123, tlv : 11998, amm : 12008, cai : 13120, jnb : 14495, cpt : 10502, dkr : 11450, cmn : 12205, rak : 10171, mem : 11120, dme : 12448, svo : 12139, aua : 8999, hav : 13123, hkg : 12467, pvg : 13123, pek : 14764, bkk : 13123, cgk : 12008, sin1 : 13123, //CANNOT USE SIN... can : 12467, syd : 12999, mel : 11998, bne : 11680, per : 11299, akl : 11926, nrt : 13123, hnd : 9839, tas : 13123, rix : 8366, ham : 12028, osl : 11811, cph : 11811, hel : 11286, arn : 10830, kef : 10056 } //Uses user input to get relevant value from airport object var airportNumber = (airport[input]); function capable() { input.toLowerCase; alert(airportNumber) if (airportNumber >= 10200) { alert("YES") } else { alert("NO") } } 
 <form id="airportForm" action="form_action.asp"> Airport: <input id="airport" type="text" name="airport"> </form> <button onclick="capable()">Submit</button> 

It seems that everything is working, but in function capable(), when it alerts airportNumber, it alerts 'undefined.' 似乎一切正常,但是在功能enabled()中,当它警告airportNumber时,它会警告“未定义”。

There is obviously a missed connection between the user's input and my list of variables. 用户输入和我的变量列表之间显然缺少连接。 Does anybody know what I have done wrong? 有人知道我做错了吗?

It's because you were reading airportNumber and input only on page load, but you need to load it upon every input, so I put it on top of the capable function: 这是因为您正在读取airportNumber并仅在页面加载时input ,但是您需要在每次输入时都加载它,因此我将其放在功能强大的功能之上:

 var form = document.getElementById("airportForm") //user input from form var airports = { lax: 12091, jfk: 14511, ewr: 11000, lga: 7003, phl: 10506, dca: 7169, iad: 11500, bos: 10083, btv: 8320, mht: 9250, pwm: 7200, npt: 2999, hpn: 6549, bdl: 9510, bwi: 10502, pit: 11500, ord: 13001, mdw: 6522, ind: 11200, lax: 12091, sfo: 11870, san: 9401, las: 14510, sea: 11901, dfw: 13401, aus: 12248, atl: 11890, pbi: 10008, mco: 12005, lhr: 12799, lgw: 10364, man: 10000, lcy: 4948, dub: 8652, snn: 10495, cdg: 13829, ory: 11975, mad: 14272, bcn: 11654, opo: 11417, lis: 12484, dxb: 13123, auh: 13451, dwc: 14764, jed: 12467, ruh: 13796, kwi: 11483, ebl: 13891, sda: 13123, tlv: 11998, amm: 12008, cai: 13120, jnb: 14495, cpt: 10502, dkr: 11450, cmn: 12205, rak: 10171, mem: 11120, dme: 12448, svo: 12139, aua: 8999, hav: 13123, hkg: 12467, pvg: 13123, pek: 14764, bkk: 13123, cgk: 12008, sin1: 13123, //CANNOT USE SIN... can: 12467, syd: 12999, mel: 11998, bne: 11680, per: 11299, akl: 11926, nrt: 13123, hnd: 9839, tas: 13123, rix: 8366, ham: 12028, osl: 11811, cph: 11811, hel: 11286, arn: 10830, kef: 10056 } //Uses user input to get relevant value from airport object function capable() { var input = document.getElementById("airport").value; var airportNumber = (airports[input]); alert(airportNumber) if (airportNumber >= 10200) { alert("YES") } else { alert("NO") } } 
 <form id="airportForm" action="form_action.asp"> Airport: <input id="airport" type="text" name="airport"> </form> <button onclick="capable()">Submit</button> 

You have several problems 你有几个问题

1) You're not getting updated airport code on button click 1)点击按钮后,您没有得到更新的机场代码

2) input variable is the html control and the the airport code itself. 2) input变量是html控件和机场代码本身。 You have to use ìnput.value 您必须使用ìnput.value

function capable() {
  var input = document.getElementById("input");
  var airportNumber = airports[input.value]; // get user airport code
    input.toLowerCase;
    alert(airportNumber)
    if (airportNumber >= 10200) {
        alert("YES")
    }
    else {
        alert("NO")
    }
}

BTW: there is no need to do a global loading of input as you do on the second line, I moved that logic to capable method 顺便说一句:不需要像第二行那样全局加载input ,我已将该逻辑移至capable方法

暂无
暂无

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

相关问题 如何获取日期输入的值并将其保存到全局变量? - How do I take the value of a date input and save it to a global variable? 如何将变量的值设置为输入字段中的用户输入? - How do I set the value of a variable to the user input in an input field? 如何在mousehover 上增加全局变量,但让它在mouseout 上恢复到正常值? - How do I increment a global variable onmousehover, but have it return to it's normal value onmouseout? 我如何使用 JavaScript 将输入从文本框中获取到变量然后打印变量的值? - How do i use JavaScript to take the input from a text box to a variable then print the value of a variable? 我该如何储存 <input> “name”作为对象的属性,然后存储匹配 <input> “value”作为该对象属性的值? - How do I store <input> “names” as properties of an object and then store the matching <input> “value” as the values of that object's properties? 如何获取用户的输入并将其存储以在 Javascript function 中使用? - How do I take a user's input and store it to be used in a Javascript function? 如何获取输入值并将其放入angularJS中的变量? - How can I take a input value and put into a variable in angularJS? 如何插入可以将用户输入作为文本的选项 - How do I insert an option that can take user input as text 我如何获取用户的输入并搜索 api? - How do i take input from user and search api? 如何获取输入字段的值并使用它来过滤数组? - How do I take the value of an input field and use it to filter an array?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM