[英]execute a javascript before submitting a form
I'm working on a project where when a user enters 13 characters in the input, then the form gets submitted automatically using JavaScript without pressing a submitted button.我正在开发一个项目,当用户在输入中输入 13 个字符时,表单将使用 JavaScript 自动提交,而无需按下提交按钮。
Now i want to add a beep sound when the user enters the 13th character in the input.现在我想在用户在输入中输入第 13 个字符时添加哔声。
<input id="text" type="text" maxlength="13" class="form-control" value='' name="order_id" autofocus>
function play() {
var audio = new Audio('beep.mp3');
audio.play();
}
$("#text").keyup(function(e) {
var length = this.value.length;
if (length == 13) {
play(); //first i need to run this function to play sound then i want to submit form after 1.5 second
this.form.submit();
e.preventDefault();
}
});
})
You can use setTimeout
.您可以使用setTimeout
。
function play() {
var audio = new Audio('beep.mp3');
audio.play();
}
$("#text").keyup(function(e) {
var length = this.value.length;
if (length == 13) {
play(); //first i need to run this function to play sound then i want to submit form after 1.5 second
setTimeout(function(){
this.form.submit();
e.preventDefault();
}, 1500);
}
});
Like this.像这样。 My code will submit after the sound has played - and not before我的代码将在声音播放后提交 - 而不是之前
No need for timeout不需要超时
Remember to stop the code from executing the play after submitting the first time, or the async function will be triggered more than once第一次提交后记得停止代码执行播放,否则async函数会被触发多次
let subbed = false; function play() { subbed = true; var audio = new Audio('https://freesound.org/data/previews/254/254819_4597795-lq.mp3'); audio.addEventListener("ended", function() { console.log("sub"); $("#form1")[0].submit(); }); audio.play(); } $("#text").keyup(function(e) { var length = this.value.length; if (length === 13 && !subbed) { play(); } })
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <form id="form1" action="https://plungjan.name/"> <input id="text" type="text" maxlength="13" class="form-control" value='' name="order_id" autofocus> </form>
You can use the ended
event on the Audio node to trigger the submission instead of hardcoding the length, so it accounts for delays in loading the sound.您可以使用 Audio 节点上的ended
事件来触发提交,而不是硬编码长度,因此它考虑了加载声音的延迟。
function playSound(callback) {
var audio = new Audio('beep.mp3');
audio.addEventListener('ended', callback);
audio.play();
}
document.querySelector('#text').addEventListener('change', function (event) {
if (event.currentTarget.value.length === 13) {
playSound(() => {
document.querySelector('form').submit();
});
}
})
You can try it in such way:你可以这样试试:
const el = document.getElementById("text") const formEl = document.getElementById("form1") const play = function(){ let audio = new Audio('beep.mp3') audio.play() } el.oninput = function() { let length = el.value.length if (length === 13) { play() //first i need to run this function to play sound then i want to submit form after 1.5 second setTimeout(() => formEl.submit(), 1500) } }
<form action="" id="form1"> <input id="text" type="text" maxlength="13" class="form-control" value='' name="order_id" autofocus> </form>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.