简体   繁体   English

如何模拟回车键复制提交键?

[英]How to simulate the Enter button to replicate the submit button?

I am making a weather application with a textarea, if you click "submit" you will see the weather results.我正在制作一个带有文本区域的天气应用程序,如果您单击“提交”,您将看到天气结果。 Now, I want to make it so you can click enter to see the data.现在,我想制作它以便您可以单击 enter 查看数据。 this is some code:这是一些代码:

    <section class="inputs">
        <input type="text" placeholder="Enter any city..." id="cityinput">
        <input type="submit" value="Submit" id="add">
        <button placeholder="submit" id="add"></button>
    </section>

This is some javascript code:这是一些 javascript 代码:

   btn.addEventListener('click', function(){

//This is the api link from where all the information will be collected

        fetch('https://api.openweathermap.org/data/2.5/weather?q='+inputval.value+'&appid='+apik)
            .then(res => res.json())

            //.then(data => console.log(data))

            .then(data => {

//Now you need to collect the necessary information with the API link. Now I will collect that information and store it in different constants.

                var nameval = data['name']
                var tempature = data['hourly']['pop']
//Now with the help of innerHTML you have to make arrangements to display all the information in the webpage.
                city.innerHTML=`Weather of <span>${nameval}<span>`
                temp.innerHTML = ` <span>${ tempature} </span>`


            })

You need to attach a listener to your textarea, if the user press enter then you execute your call (here simulated by an alert) and you prevent the default in order to don't go in a new line.您需要将一个侦听器附加到您的文本区域,如果用户按下enter键,那么您将执行您的呼叫(此处通过警报模拟)并阻止默认设置,以免 go 换行。 In any other case, just don't take any action在任何其他情况下,不要采取任何行动

 const textArea = document.querySelector('textarea'); textArea.addEventListener('keydown', e => { if (e.key === 'Enter') { window.alert('Sending data...'); e.preventDefault(); } });
 <textarea placeholder="Type and press enter"></textarea>

You can just put the submit code in a function, and call the function in both the cases:您可以将submit代码放在 function 中,并在这两种情况下调用 function:

function submitAction() {
  fetch('https://api.openweathermap.org/data/2.5/weather?q='+inputval.value+'&appid='+apik)
  .then(res => res.json())
  .then(data => {
      const nameval = data['name']
      const tempature = data['hourly']['pop']
      city.innerHTML=`Weather of <span>${nameval}<span>`
      temp.innerHTML = ` <span>${ tempature} </span>`
   });
}

Then:然后:

  if (e.key === 'Enter') {
    submitAction();
    e.preventDefault();
  }

And:和:

 btn.addEventListener('click', () => submitAction());

You could use a HTML form and use the form submit event :您可以使用 HTML 表单并使用表单提交事件

<form id="form">
    <input type="text" placeholder="Enter any city..." id="cityinput" />
    <button type="submit">Submit</button>
</form>

Inside the event listener you can then read the value from the input once the submit event is triggered.在事件侦听器中,一旦submit事件被触发,您就可以从输入中读取值。 Alternatively you could go looking for the input value inside the event object.或者,您可以 go 在event object 中查找输入值。

var form = document.getElementById('form');
form.addEventListener('submit', function(event) {
  const inputValue = document.querySelector('input').value;
        event.preventDefault();
        fetch('https://api.openweathermap.org/data/2.5/weather?q='+inputValue+'&appid='+apik)
            .then(res => res.json())
            .then(data => {
                var nameval = data['name']
                var tempature = data['hourly']['pop']
                city.innerHTML=`Weather of <span>${nameval}<span>`
                temp.innerHTML = ` <span>${ tempature} </span>`
            })
});

You need to create listener for keydown event and check if clicked key is enter:您需要为 keydown 事件创建侦听器并检查是否输入了单击的键:

const textarea = document.querySelector(".some-class");

textarea.addEventListener("keydown", function(e) {
    if(e.key === "Enter") {
        // your code
    }
});
<textarea class="some-class"></textarea>

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

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