简体   繁体   English

如何在 getLink function 中使用来自 html 表单的输入?

[英]How do I use inputs from my html form in a getLink function?

So this is my first question, and it's also the first code I'm trying to write.所以这是我的第一个问题,也是我尝试编写的第一个代码。

I basically want to make a form to collect values like the Departing Airport Code.我基本上想制作一个表格来收集诸如出发机场代码之类的值。 An input for the Departing Airport Code would look like 'ams'.出发机场代码的输入看起来像“ams”。

This value is needed to customize the URL which the getLink function opens in a new tab.需要此值来自定义 getLink function 在新选项卡中打开的 URL。

I've read other questions and tried a lot of things.我已经阅读了其他问题并尝试了很多事情。 But the input doesn't appear in the URL that gets opened in the new tab.但是输入没有出现在新选项卡中打开的 URL 中。

In this example, I'm trying to get the input with '+dairport+' placed in the getLink URL.在这个例子中,我试图在 getLink URL 中获取带有“+dairport+”的输入。

How can I get the input data from the form and use it in the URL that gets opened when I click the button?如何从表单中获取输入数据并在单击按钮时打开的 URL 中使用它?

 document.getElementById("dairport").value = "dairport"; function getLink(dairport) { window.open("https://www.skyscanner.nl/transport/flights/+dairport+/jfk/210701/210714/?adults=1&adultsv2=1&cabinclass=business&children=0&childrenv2=&destinationentityid=27537542&inboundaltsenabled=false&infants=0&originentityid=27536561&outboundaltsenabled=false&preferdirects=false&preferflexible=false&ref=home&rtn=1"); }
 <form name="box"> <label for="dairport">Departing Airport Code:</label><br> <input type="text" id="dairport" name="dairport"><br> <label for="aairport">Arriving Airport Code:</label><br> <input type="text" id="aairport" name="aairport"><br> <label for="ddate">Departing Date:</label><br> <input type="date" id="ddate" name="ddate"><br> <label for="adate">Arriving Date:</label><br> <input type="date" id="adate" name="adate"><br> <label for="class">Class:</label><br> <select id="class" name="class"><br> <option value="business">Business</option> <option value="first">First</option> </select><br> <button onclick="getLink(dairport);">Launch</button> </form>

document.getElementById("dairport").value = "dairport"; will set the value of the input with the id of dairport .将使用dairport的 id 设置输入的值。 What you want to do is get the value the user entered.您要做的是获取用户输入的值。 That is just document.getElementById("dairport").value and should be inside your getLink() function.这只是document.getElementById("dairport").value并且应该在您的getLink() function 中。

You also don't really need a form since you aren't submitting any information to a server or similar.您也不需要form ,因为您没有向服务器或类似服务器提交任何信息。

Then you can use template literals to include the value of the departingAirport variable in the link to open.然后,您可以使用模板文字在要打开的链接中包含departingAirport变量的值。 What you are doing is adding the literal string +airport+ in the URL which is no good, you need to put the value of the variable (which is what template literals help with).您正在做的是在 URL 中添加文字字符串+airport+这不好,您需要输入变量的值(这是模板文字的帮助)。

 function getLink() { let departingAirport = document.getElementById("dairport").value; window.open(`https://www.skyscanner.nl/transport/flights/${departingAirport}/jfk/210701/210714/?adults=1&adultsv2=1&cabinclass=business&children=0&childrenv2=&destinationentityid=27537542&inboundaltsenabled=false&infants=0&originentityid=27536561&outboundaltsenabled=false&preferdirects=false&preferflexible=false&ref=home&rtn=1`); }
 <label for="dairport">Departing Airport Code:</label><br> <input type="text" id="dairport" name="dairport"><br> <label for="aairport">Arriving Airport Code:</label><br> <input type="text" id="aairport" name="aairport"><br> <label for="ddate">Departing Date:</label><br> <input type="date" id="ddate" name="ddate"><br> <label for="adate">Arriving Date:</label><br> <input type="date" id="adate" name="adate"><br> <label for="class">Class:</label><br> <select id="class" name="class"><br> <option value="business">Business</option> <option value="first">First</option> </select><br> <button onclick="getLink();">Launch</button>

@andrew Lhor Thanks for the answer again! @andrew Lhor 再次感谢您的回答! I tried 2 window.open s with 2 different Arriving Airports but it doesn't seem to work (only the first window is opening).我尝试了 2 个window.open和 2 个不同的到达机场,但它似乎不起作用(只有第一个 window 正在打开)。

It now looks like this:现在看起来像这样:

function getLink() {
                 
let departingAirport = document.getElementById("dairport").value;
let arrivingAirport = document.getElementById("aairport").value;
let arrivingAirport2 = document.getElementById("aairport2").value;
let classSeat = document.getElementById("class").value;
let departingDate = document.getElementById("ddate").value;
let arrivingDate = document.getElementById("adate").value;
             
             window.open(`https://www.skyscanner.nl/transport/flights/${departingAirport}/${arrivingAirport}/${departingDate}/${arrivingDate}/?adults=1&adultsv2=1&cabinclass=${classSeat}&children=0&childrenv2=&destinationentityid=27537542&inboundaltsenabled=false&infants=0&originentityid=27536561&outboundaltsenabled=false&preferdirects=false&preferflexible=false&ref=home&rtn=1`);
                          
             window.open(`https://www.skyscanner.nl/transport/flights/${departingAirport}/${arrivingAirport2}/${departingDate}/${arrivingDate}/?adults=1&adultsv2=1&cabinclass=${classSeat}&children=0&childrenv2=&destinationentityid=27537542&inboundaltsenabled=false&infants=0&originentityid=27536561&outboundaltsenabled=false&preferdirects=false&preferflexible=false&ref=home&rtn=1`);
             
}

And this:和这个:

<label for="dairport">Departing Airport:</label><br>
<input type="text" id="dairport" name="dairport"><br>

<label for="aairport">Arriving Airport:</label><br>
<input type="text" id="aairport" name="aairport"><br>

<label for="aairport2">Arriving Airport2:</label><br>
<input type="text" id="aairport2" name="aairport2"><br>
    
<label for="ddate">Departing Date:</label><br>
<input type="text" id="ddate" name="ddate"><br>

<label for="adate">Arriving Date:</label><br>
<input type="text" id="adate" name="adate"><br>

<label for="class">Class:</label><br>

<select id="class" name="class"><br>

  <option value="business">Business</option>
  <option value="first">First</option>

</select><br>

<button onclick="getLink();">Launch</button>

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

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