Javascript 单击按钮后,计算x秒,然后使用jquery重定向到特定链接
我想做的正是我在题目中提到的。我有一个带有提交按钮的表单。从用户单击submit按钮开始计数5秒后,是否可以使用jQuery将用户重定向到特定的链接页面 下面的代码正是您想要的:Javascript 单击按钮后,计算x秒,然后使用jquery重定向到特定链接,javascript,jquery,Javascript,Jquery,我想做的正是我在题目中提到的。我有一个带有提交按钮的表单。从用户单击submit按钮开始计数5秒后,是否可以使用jQuery将用户重定向到特定的链接页面 下面的代码正是您想要的: $('#button').click(function() { setTimeout(function() { window.location.href = 'http://new-url'; }, 5000); }) 下面的代码正是您想要的: $('#button').click(f
$('#button').click(function() {
setTimeout(function() {
window.location.href = 'http://new-url';
}, 5000);
})
下面的代码正是您想要的:
$('#button').click(function() {
setTimeout(function() {
window.location.href = 'http://new-url';
}, 5000);
})
您可以使用setTimeout和window.location
setTimeout(function(event) {
window.location.href = url
}, 5000);
您可以使用setTimeout和window.location
setTimeout(function(event) {
window.location.href = url
}, 5000);
这听起来像是JavaScript的
setTimeout
功能的工作。
您需要做的第一件事是使用event.preventDefault()
防止表单立即提交(默认行为),否则不会执行任何操作
查看这个例子并给出评论,应该可以为您提供更多的信息
<form method="post">
<input type="text" name="text-field" placeholder="Enter something" />
<button type="submit">Submit form</button>
</form>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script type="text/javascript">
$(function() {
$('form').on('submit', function(event) { // pass an event argument so we can call preventDefault
event.preventDefault(); // call event.preventDefault to stop form submitting immediately
setTimeout(function() { // Set up a setTimeout operation
console.log('form submitted after timeout');
window.location.href = '/';
}, 5000); /// 5000 ms == 5s
});
});
提交表格
$(函数(){
$('form')。在('submit')上,函数(事件){//传递一个事件参数,以便调用preventDefault
event.preventDefault();//调用event.preventDefault立即停止表单提交
setTimeout(函数(){//设置setTimeout操作
log(“超时后提交的表单”);
window.location.href='/';
},5000);///5000毫秒==5s
});
});
这听起来像是JavaScript的
setTimeout
功能的工作。
您需要做的第一件事是使用event.preventDefault()
防止表单立即提交(默认行为),否则不会执行任何操作
查看这个例子并给出评论,应该可以为您提供更多的信息
<form method="post">
<input type="text" name="text-field" placeholder="Enter something" />
<button type="submit">Submit form</button>
</form>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script type="text/javascript">
$(function() {
$('form').on('submit', function(event) { // pass an event argument so we can call preventDefault
event.preventDefault(); // call event.preventDefault to stop form submitting immediately
setTimeout(function() { // Set up a setTimeout operation
console.log('form submitted after timeout');
window.location.href = '/';
}, 5000); /// 5000 ms == 5s
});
});
提交表格
$(函数(){
$('form')。在('submit')上,函数(事件){//传递一个事件参数,以便调用preventDefault
event.preventDefault();//调用event.preventDefault立即停止表单提交
setTimeout(函数(){//设置setTimeout操作
log(“超时后提交的表单”);
window.location.href='/';
},5000);///5000毫秒==5s
});
});
为了实现您想要实现的目标,您需要使用<代码>设置超时是一个采用两个参数的函数:
- 回调函数
- 超时以毫秒为单位
函数
是在给定事件发生时执行的函数。在我们的例子中,事件是经过给定的时间量。这是您的回调的外观:
function redirect() {
window.location.href = 'http://www.google.com';
}
超时
超时是函数的第二个参数。它定义了调用回调之前要经过的时间量。在我们的例子中,在执行回调之前需要等待5秒,因此超时时间将为5000毫秒
用法
或
无论哪种方式,在解决方案周围包装一个函数
,并调用onclick上的函数
,这都是一种优雅的做法,您需要使用它来实现您想要实现的目标<代码>设置超时
是一个采用两个参数的函数:
- 回调函数
- 超时以毫秒为单位
回拨
回调函数
是在给定事件发生时执行的函数。在我们的例子中,事件是经过给定的时间量。这是您的回调的外观:
function redirect() {
window.location.href = 'http://www.google.com';
}
超时
超时是函数的第二个参数。它定义了调用回调之前要经过的时间量。在我们的例子中,在执行回调之前需要等待5秒,因此超时时间将为5000毫秒
用法
或
无论哪种方式,在解决方案周围包装一个函数
,并在onclick上调用函数
,都是非常优雅的