javascript 使 onclick 功能与 Safari Mobile (iOS) 上的 ENTER 和“Go”按钮一起使用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16263331/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 03:57:40  来源:igfitidea点击:

Make onclick function work with ENTER and "Go" button on Safari Mobile (iOS)

javascripthtmljavascript-eventsonclickbuttonclick

提问by MultiformeIngegno

I have a form in which the button calls a function onclick:

我有一个表单,其中按钮调用 onclick 函数:

<form>
    <input type="text" id="start" name="start">
    <input type="button" onclick="calcRoute();" value="Go">
</form>

function calcRoute() {
    var start = document.getElementById('start').value;
      var end = "My Address in Rome";
      var request = {
        origin:start,
        destination:end,
        travelMode: google.maps.DirectionsTravelMode.TRANSIT
      };
    directionsService.route(request, function(response, status) {
      if (status == google.maps.DirectionsStatus.OK) {
        directionsDisplay.setDirections(response);
      }
    });
}

It works properly on every browser if you click on the "Go" button (<input type="button">). It doesn't if I fill the <input type="text">and press ENTER on my keyboard (or for example push the "Go" button in iOS keyboard). So: it works if I push the input type="button", not if I press ENTER from keyboard. How can I make it work that way too? Thanks in advance

如果您单击“开始”按钮 ( <input type="button">),它可以在每个浏览器上正常工作。如果我填写<input type="text">并按下键盘上的 ENTER(或例如按下 iOS 键盘中的“开始”按钮),则不会。所以:如果我按下输入类型 =“按钮”,它会起作用,而不是我从键盘上按 ENTER。我怎样才能让它以这种方式工作?提前致谢

EDIT: Not jQuery or other framework please. :)

编辑:请不要使用 jQuery 或其他框架。:)

回答by PeeHaa

You should really just remove that inline javascript and make that button into a real submit button. After that you can easily capture the form being submitted (wether be enter key or by submit button):

您真的应该删除该内联 javascript 并将该按钮变成真正的提交按钮。之后,您可以轻松捕获正在提交的表单(无论是输入键还是通过提交按钮):

form.addEventListener('submit', calcRoute);

Demo: http://jsfiddle.net/ghK4P/

演示:http: //jsfiddle.net/ghK4P/

回答by user2243116

<input type="text" id="start" name="start" onkeypress="return calcRoute(event)">

now modify your script as follows:

现在修改你的脚本如下:

function calcRoute(e) {
    if (e.keyCode == 13) {
        //your code
    }
}

回答by MultiformeIngegno

I found the solution :)

我找到了解决方案:)

<form onsubmit="calcRoute(); return false">
    <input type="text" id="start" name="start">
    <input type="submit" value="Vai">
</form>

回答by user2243116

try this:

试试这个:

<form onsubmit="calcRoute()">
    <input type="text" id="start" name="start">
    <input type="button" onclick="calcRoute();" value="Go">
</form>

i'm sure it'll work with both

我相信它会同时适用于两者

回答by SHUNMUGA RAJ PRABAKARAN

Probably add this in your code if you using jquery

如果您使用 jquery,可能会将此添加到您的代码中

$(document).ready(function() {
$("#start").bind('keyup',function(event){
    if(event.keyCode == 13){
        $("#start").click();
    }
});
});

it will trigger ur button click function on pressing enter key in your text box.

它会在您的文本框中按下 Enter 键时触发您的按钮单击功能。

If you not using jquery try this one

如果你不使用 jquery 试试这个

<input type="text" id="start" name="start" onkeydown="if (event.keyCode == 13) document.getElementById('fire_calRoute').click()">
<input type="button" id='fire_calRoute' onclick="calcRoute();" value="Go">

try this, hope it will solve your problem.

试试这个,希望它能解决你的问题。