[英]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.