javascript上基于表单提交的自定义URL的setTimeout()
如何修复?我一整天都在努力解决这个问题——查看其他帖子,尝试不同的方法。非常感谢您的帮助……~新手 这一行javascript上的setTimeout()会中断代码,导致为自定义URL传递空值 计划是javascript上基于表单提交的自定义URL的setTimeout(),javascript,php,jquery,html,forms,Javascript,Php,Jquery,Html,Forms,如何修复?我一整天都在努力解决这个问题——查看其他帖子,尝试不同的方法。非常感谢您的帮助……~新手 这一行javascript上的setTimeout()会中断代码,导致为自定义URL传递空值 计划是 用户在文本字段中键入一个值 该值将显示在网页上 暂停4秒后,用户将根据在文本字段中键入的值自动重定向到新网页。。。此值通过提交表单传递到URL 。。。卡在#3。页面暂停4秒钟,但没有向URL传递任何值,它将重定向到主目录 有关守则: <form id="type_form" action="
<form id="type_form" action="html-echo.php" method="post" onsubmit="setTimeout(function() { location.href='http://localhost:8888/vanity/site/' + document.getElementById('nav').value; return false; }, 4000);">
<input id="nav" maxlength="10" type="text" name="nav" autofocus placeholder="TYPE YOUR CHOICE... (spelling counts)">
<script>
if (!("autofocus" in document.createElement("input"))) {
document.getElementById("nav").focus();
}
</script>
<button class="send" type="submit">SEND</button></form>
if(!(.createElement(“输入”)中的“自动聚焦”){
document.getElementById(“nav”).focus();
}
发送
谢谢大家! 您可以尝试以下方法:
<form id="type_form"
action="html-echo.php" method="post"
onsubmit="var redir='http://localhost:8888/vanity/site/'
+ document.getElementById('nav').value;
setTimeout(function() { location.href=redir; }, 4000);">
像这样试试看
<form id="type_form" action="html-echo.php" method="post">
<input id="nav" maxlength="10" type="text" name="nav" placeholder="TYPE YOUR CHOICE... (spelling counts)" autofocus />
<button class="send" type="submit">SEND</button>
</form>
<script type="text/javascript">
if (!( document.activeElement.id || document.activeElement.id == 'nav')) {
document.getElementById("nav").focus();
}
document.getElementById('type_form').onsubmit = function() {
setTimeout(function() {
document.location.href = 'http://localhost:8888/vanity/site/' + document.getElementById('nav').value;
}, 4000);
return false;
}
</script>
发送
if(!(document.activeElement.id | | document.activeElement.id=='nav')){
document.getElementById(“nav”).focus();
}
document.getElementById('type_form')。onsubmit=function(){
setTimeout(函数(){
document.location.href=http://localhost:8888/vanity/site/'+document.getElementById('nav').value;
}, 4000);
返回false;
}
另外,在超时时返回false并不阻止表单提交,您必须将false right返回。我认为在您的情况下,您必须使用debounce和throttle,而不是直接使用setTimeout()。检查样本: