简体   繁体   English

提交时仅调用一次函数

[英]Calling a function only one time while submiting

I am trying to submit a form and while submitting I call a function which is myOwnFun and it runs. 我正在尝试提交表单,并且在提交时我调用了一个函数myOwnFun并运行。 But if I keep pressing enter for long time, form submit repeats which I don't want. 但是,如果我长时间按Enter键,则表单提交会重复,这是我不希望的。

Here is the code.. 这是代码。

<form class="user__answer-form">
     <input type="text" name="useranswer" class="user__answer">
</form> 
const form = document.querySelector('.user__answer-form');
const formAnswer = document.querySelector('.user__answer');

formAnswer.focus();

form.addEventListener('submit', function(e) {
   e.preventDefault();
   myOwnFun();
});


function myOwnFun() {
   console.log('show text one time in one Enter')
}

I want to run this function only one time when I press enter even if I pressed for longer time. 我只想按一次Enter键即可运行此功能,即使我按了更长的时间也是如此。 And I need to submit the form many different times. 我需要多次提交表格。

 const form = document.querySelector('.user__answer-form'); const formAnswer = document.querySelector('.user__answer'); //create a counter to track key down events let count = 0; formAnswer.focus(); form.addEventListener('submit', function(e) { e.preventDefault(); //call your function only if count is 0 //ie is first time enter key is pressed //if key is pressed for a long time count > 0 // this condition fails if(count == 1){ myOwnFun(); } }); //fires on any key is pressed on form element form.addEventListener('keydown', function(e) { // for cross browser compatibility const keyCode = e.which || e.keyCode; if(keyCode == 13){ //if enter key is pressed update count count ++; } }); //fires on key is released on form element form.addEventListener('keyup', function(e) { const keyCode = e.which || e.keyCode; //if enter key is released reset the count if(keyCode == 13){ count = 0 } }); function myOwnFun() { console.log('show text one time in one Enter'); } 
 <form class="user__answer-form" > <input type="text" name="useranswer" class="user__answer"> </form> 

首先在脚本中然后在代码中使用e.preventDefault()方法,这将禁用表单和执行代码的默认功能。

Try this: 尝试这个:

const form = document.querySelector('.user__answer-form');
const formAnswer = document.querySelector('.user__answer');

formAnswer.focus();

form.addEventListener('submit', function(e) {
   e.preventDefault();
   myOwnFun();
});


function myOwnFun() {
 var executed = false;
    return function() {
        if (!executed) {
            executed = true;
            console.log('show text one time in one Enter')
        }
    };

}

you can use that: form.removeEventListener("submit", yourFunction); 您可以使用: form.removeEventListener("submit", yourFunction);

I think you'll have to change function(e) to a "real function" to make it work 我认为您必须将function(e)更改为“ real function”以使其起作用

As a "throw a library/package at it" solution, Lodash has a once function that restricts the function to one call per page load: https://lodash.com/docs/4.17.11#once . 作为“向其扔库/软件包”解决方案,Lodash具有once函数,该函数将函数限制为每次页面加载仅一次调用: https ://lodash.com/docs/4.17.11#once。 Might be a bit overkill, especially if you just need it in the one line, so I would recommend it as a last resort, even if you go with the standalone package. 可能有些矫kill过正,尤其是如果您只需要一行的话,因此即使您使用独立软件包,我也建议将其作为最后的选择。

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

相关问题 使用AJAX仅提交一种表单 - Submiting only one form with AJAX JavaScript代码仅调用一次代码隐藏函数 - JavaScript code calling codebehind function only one time 在 useEffect 之外只调用一次函数反应原生 - calling a function only one time outside of useEffect react native 一次只调用一次 function - Calling the function only once in a time 在提交和调用servlet之前调用javascript验证功能 - Calling javascript validation function before submiting and calling servlet JavaScript 调用函数 4 次,但我只需要执行一次函数 - JavaScript calling function 4 times but I need to execute function only one time 通过addEventListener调用一个function,但是function只能工作一次,不能再调用 - Calling a function by addEventListener, but the function only work one time, can not call it again 当我一键调用两个函数时(onclick)只重定向 function 正在执行。? 需要解决方案 - while I'm calling two functions on one click(onclick) only redirect function is executing.? need solution for it 调用函数仅返回一次false之后,表单未执行提交 - form is not doing submit after calling function return false only one time 我正在从ASP.net后面的代码中调用Jquery函数,但是它只显示一次 - I am calling the Jquery function from code behind in asp net but it showing for one time only not for other
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM