简体   繁体   English

Google App脚本-在输入文本框中输入密钥-如何捕获或禁用?

[英]Google App Script - Enter key in input textbox - how to catch or disable?

I have an html page published as a web app through Google app scripts. 我有一个通过Google应用程序脚本发布为网络应用程序的html页面。 On the page is an input box (textbox) the user types a number to search for. 在页面上是输入框(文本框),用户键入要搜索的数字。 If they click the 'search' button beside the box, it works fine. 如果他们单击框旁边的“搜索”按钮,则效果很好。 If they type the number and then press 'enter' on the keyboard, it throws a 400 error ('The requested URL was not found on this server. '). 如果他们键入数字,然后在键盘上按'enter',则会引发400错误(“在此服务器上找不到所请求的URL。”)。

input area code: 输入区号:

<input type="text" id="claimSearchBox" class="form-control"  placeholder="Search" />
<input type="button" id="btnsearchClaims" class="btn-md btn-success" onclick="getOrders" value="Search for a Claim"  />

javascript: (this works for the button) javascript :(此按钮适用)

document.getElementById("btnsearchClaims").addEventListener("click", getOrders);

(this does not work for the input box) (这不适用于输入框)

document.getElementById("claimSearchBox").addEventListener("keyup", function(event) {
event.preventDefault();
console.log('event:  ' + event.keyCode);
if (event.keyCode == 13) {
  console.log('correct key');
  getOrders();
}
});

I also tried this, which shows what keys are pressed, but as soon as I press enter, it goes to the 400 error. 我还尝试了此操作,该操作显示了按了哪些键,但是一旦按Enter键,它就会转到400错误。

$(document).ready(function(){
  $("claimSearchBox").keyup(function(e){
  console.log(e.keycode);
    console.log("worked");

  });
});

Any suggestions? 有什么建议么? Is there a way to disable the 'enter' if I can't redirect it to the getOrders()? 如果无法将其重定向到getOrders(),是否可以禁用“输入”?

**Edited to add...if I don't have any programming for the input box and the user presses enter - it goes to the error (page is blank - error is shown in console). **已编辑以添加...如果我对输入框没有任何编程,并且用户按Enter键,则会出现错误(页面为空白-控制台中显示错误)。

***Found the problem: Turns out it was an issue of this input box being the only input box on the form. ***发现了问题:原来这是此输入框是表单上唯一的输入框的问题。 When the user hits enter, it automatically submits the form! 当用户按下Enter键时,它将自动提交表单! I found the issue/solution here: Why does forms with single input field submit upon pressing enter key in input 我在这里找到问题/解决方案: 为什么在输入中按下回车键时提交具有单个输入字段的表单

This may help you get further with your code. 这可以帮助您进一步了解代码。

I see that you are invoking jQuery. 我看到您正在使用jQuery。

$(document).ready(function(){

Can you confirm that the page you are displaying is properly loading jQuery? 您可以确认所显示的页面正在正确加载jQuery吗?

Your html should have something like this present. 您的html应该有这样的东西。

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>

If you use your debugger and successfully enter your readythen you have jQuery loaded. 如果您使用调试器并成功输入ready,那么您已加载jQuery。

With jQuery, you could simplify your event handling. 使用jQuery,您可以简化事件处理。

Replace 更换

document.getElementById("btnsearchClaims").addEventListener("click", getOrders);

With

$("#btnsearchClaims").on("click", getOrders);

Replace 更换

document.getElementById("claimSearchBox").addEventListener("keyup", 
function(event) {

With

$("#claimSearchBox").on("keyup", function(event)...

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

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