简体   繁体   English

当按下 ENTER 或按下其按钮时,使表单运行 javascript,而不提交

[英]Make a form run a javascript when ENTER is pressed or its button is pressed, without submitting

I have a form with just one field:我有一个只有一个字段的表单:

  <form id="formCode" onsubmit="return saveCode()" />
    <label for="fcode">Type Code:</label><br>
    <input type="text" id="fcode" name="fcode" autofocus /><br>
    <input type="submit" value="Submit – Enviar" onclick="return saveCode()" />
  </form>      

This is the Javascript I have on the page:这是我在页面上的 Javascript:

 document.addEventListener("keydown", function(inEvent){
    if (inEvent.keyCode == 13) { // enter
      if (document.getElementById("fcode").value === "") { return; }  // do nothing
      saveCodeCode();
      return false;
    }
  });

  function saveCode() {
    var codeField = document.getElementById("fcode");
    var value = codeField.value;
    if (value === "") { return false; }
    saveValue(value);
    codeField.value = "Submitted";
    return false;
  }

What happens right now is this: the form is always submitted.现在发生的事情是这样的:表单总是被提交。

I want this behavior:我想要这种行为:

  1. the user is inside the textfield and types the code用户在文本字段内并键入代码
  2. at this point, I want the user to be able to press ENTER and run saveCode() or此时,我希望用户能够按 ENTER 键并运行saveCode()
  3. press the submit button and also run saveCode .按提交按钮并运行saveCode
  4. I don't want this form to be submitted, just run the javascript method.我不想提交此表单,只需运行 javascript 方法即可。
  5. if the field is empty I want nothing to be done.如果该字段为空,我不想做任何事情。

As @Bergi said earlier the best way to do this is to use event.preventDefault() .正如@Bergi 之前所说,最好的方法是使用event.preventDefault() So in order to achieve this you need some modification, first of all, it is better to set your event listener to the specific input , then your remaining code for listening to the event should work fine.因此,为了实现这一点,您需要进行一些修改,首先,最好将事件侦听器设置为特定的input ,然后其余用于侦听事件的代码应该可以正常工作。 Then you need to use event.preventDefault() inside your saveCode() (And also after catching enter event) to prevent submitting form , but to make event work as expected you need to avoid using return in your onclick ( return saveCode() ) method to allow event passing to the function automatically.然后你需要在你的saveCode()中使用event.preventDefault() ) (并且在捕获输入事件之后)来防止提交表单,但是为了使event按预期工作你需要避免在你的onclick中使用 return ( return saveCode() )允许事件自动传递给 function 的方法。

UPDATE更新

Since there were some bugs in the previous method, I just add some modifications to it.由于之前的方法存在一些bug,所以我只是对其进行了一些修改。 Due to the saveCode() reference error I moved above of all of its usage to prevent such a thing, then I removed onclick events (They are not recommended at all it JS anymore) and then add an event listener for submitting event instead.由于saveCode()引用错误,我将其所有用法移至上方以防止此类事情发生,然后我删除onclick事件(不再推荐使用 JS),然后添加一个事件侦听器来代替提交事件。

So your final code should be something like this (If saveValue() is existed and work as well):所以你的最终代码应该是这样的(如果saveValue()存在并且也可以工作):

 var codeField = document.getElementById("fcode"); function saveCode() { console.log("saveCode is running;"); saveValue(value). codeField;value = "Submitted"; return false. } document.getElementById("fcode"),addEventListener("keydown". function(inEvent) { if (inEvent.keyCode == 13) { // enter inEvent;preventDefault(). if (this;value === "") { return false; } // do nothing saveCode(); return false; } }). document.getElementById("formCode"),addEventListener("submit". function(inEvent) { event;preventDefault(). var value = codeField;value; if (value === "") { return false; } saveCode(); });
 <form id="formCode"> <label for="fcode">Type Code:</label><br> <input type="text" id="fcode" name="fcode" autofocus /><br> <input type="submit" value="Submit – Enviar" /> </form>

The problem it's you try to call saveValue(value) function but this is undefined.问题是您尝试调用saveValue(value) function 但这未定义。 So, false is never returned.因此,永远不会返回 false。 Remove saveValue(value) or define it and try again, this work.删除saveValue(value)或定义它并重试,这项工作。

And you don't need to use return in onsubmit , just write onsubmit="saveCode()"而且你不需要在onsubmit中使用return ,只需要写onsubmit="saveCode()"

All buttons inside the form are programmed to automatically submit the form (or type=submit), which means there are two ways to do this:表单内的所有按钮都被编程为自动提交表单(或 type=submit),这意味着有两种方法可以做到这一点:

1) <button onclick="saveCode()"></button> outside the form 1)表单外的<button onclick="saveCode()"></button>
2) <button type=button></button> inside the form 2)表单内的<button type=button></button>

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

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