简体   繁体   English

在输入框中按下Enter键时调用JavaScript函数

[英]Calling JavaScript function on hitting enter key in input box

Although I am able to call Javascript function on hitting the enter key. 虽然我可以按Enter键来调用Javascript函数。 I am calling a function shortIt() when user hits the enter key, shortIt() takes the text from input box and makes a request to Google URL Shortener API which returns a short URL, but the generated response is visible for only some seconds. 我在用户按下Enter键时调用了shortIt()函数, shortIt()从输入框中获取了文本,并向Google URL Shortener API发出了返回短URL的请求,但生成的响应仅持续了几秒钟。 I am showing the response in a div. 我在div中显示响应。

Seems to be very weird problem, code is here https://s3-ap-southeast-1.amazonaws.com/s3freebucket/URLShortner/url-shortner.html 似乎是一个很奇怪的问题,代码在这里https://s3-ap-southeast-1.amazonaws.com/s3freebucket/URLShortner/url-shortner.html

But when I click on shortIt button to short the url. 但是,当我单击shortIt按钮以缩短网址时。 It works fine and the response text stays in the div. 它工作正常并且响应文本保留在div中。

This is because on pressing the enter key, the form gets submitted. 这是因为在按Enter键时, form提交。 When this happens you will notice the page reloading. 发生这种情况时,您会注意到页面正在重新加载。 This is a default behaviour and is the consequence of using <form> . 这是默认行为,是使用<form> On form submission, the page in the forms action attribute is loaded. 在提交表单时,将加载表单action属性中的页面。 In this case the action attribute of the <form> is not set and so the page you are on is just reloaded, thus the script stops running and the page reloads. 在这种情况下,未设置<form>action属性,因此只重载了您所在的页面,因此脚本停止运行并重新加载了页面。

Two straight forward options for fixing this: 解决此问题的两个简单方法:

  1. (Simplest) Remove the form tag - it is not used to submit anything so removing it should leave things still working (最简单的方法)删除表单标签-它不用于提交任何内容,因此删除它应该会使一切仍然有效

  2. Prevent the default action when the form submit event is fired. 触发表单submit事件时,请阻止默认操作。

    With JQuery: 使用JQuery:

    $('#myForm').on('submit', function (evt) { evt.preventDefault(); // prevents form submission });

Where myForm is the ID of your form tag. 其中myForm是您的表单标签的ID。 You will need add an id attribute to your existing html form tag: <form id="myForm" class="form-horizontal"> 您将需要在现有的html表单标签中添加一个id属性: <form id="myForm" class="form-horizontal">

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

相关问题 用JavaScript按下Enter键时调用函数 - Calling a function when enter key is pressed with JavaScript 为什么带有输入文本框和在Enter键上调用javascript函数的按钮的表单? - Why does a form with an input text-box and a button which calls a javascript function on enter key press? 当用户按下回车键时,输入框是否可以运行功能? - Is there a way for a input box to run a function when the user presses the enter key? 在文本框中按Enter键后的javascript功能 - javascript function after pressing enter key on text box 如何通过按回车键关注同一列中的下一行输入 - How to focus on next row input in the same column by hitting enter key 编辑文本输入时,如何在不使用表单的onSubmit处理程序的情况下按“ Enter”键执行特定的JavaScript操作? - When editing a text input, how do I do a specific JavaScript action on hitting “Enter” key without the form's onSubmit handler? 通过在没有jQuery全局事件的文本框中输入来调用JavaScript函数 - Call a JavaScript function by hitting enter in a textbox without global events of jQuery 按下Enter键不会以HTML形式调用javascript函数 - Hitting enter does not invoke javascript function in HTML form 通过按“ Enter”键发送消息 - Send message by hitting 'Enter' key 在文本框中接受JavaScript中的Enter键 - Accept enter key in JavaScript inside a text box
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM