Javascript 使用Js在html表单中输入正确密码时导航到url
我试图找到一种方法,当用户在我的表单中输入正确的密码时,导航到url,并在他们输入错误密码时显示错误警报。我是js的新手,我使用了以下两个链接:&来组合我当前的代码。我目前的代码是:Javascript 使用Js在html表单中输入正确密码时导航到url,javascript,html,forms,Javascript,Html,Forms,我试图找到一种方法,当用户在我的表单中输入正确的密码时,导航到url,并在他们输入错误密码时显示错误警报。我是js的新手,我使用了以下两个链接:&来组合我当前的代码。我目前的代码是: <form class="hero-form"> <fieldset> <div class="row-fluid">
<form class="hero-form">
<fieldset>
<div class="row-fluid">
<label>Enter your passcode</label>
<input type="password" name="password" maxlength="8" class="span7" id="password" placeholder="e.g. 12345678" required/>
<button class="btn btn-info btn-large span5" id="joe_btn" input type="submit" value="Submit" onclick="if (document.getElementById('password').value == '12345678') location.href = 'http://example.url.here'; else alert('Please check your passcode and try again');">Enter</button>
</div>
</fieldset>
</form>
有了这个,它就不起作用了
location.href = 'http://your.url.here'
任何帮助都将不胜感激。请为您的回复发布完整的代码,因为我是js的绝对初学者,如果您只发布代码片段,很容易遗漏一些内容
谢谢你,好几堆
window.location.href = 'http://your.url.here';
请注意,我将窗口放在位置的前面
请注意,我将窗口放在位置的前面
请注意,我将窗口放在位置的前面
请注意,我将窗口放在位置的前面。您将
位置设置得很好。href
但问题是,在浏览器有机会导航到新页面之前,您的表单会被提交(因为用户单击了type=“submit”
按钮)。提交表单会导致浏览器开始对表单操作的新导航,这将取消您通过设置location.href
启动的导航。在您的代码中,表单的操作尚未设置,因此它默认为您已经在的同一页面。实际上,浏览器将导航回同一页面,因此看起来好像什么也没有发生——尽管您可能会注意到表单的内容已出现在浏览器的导航栏中
您需要防止浏览器在单击按钮时自动提交表单。一种方法是添加returnfalse
到您的onclick
操作的末尾:
onclick="if (document.getElementById('password').value == '12345678') location.href = 'http://example.url.here'; else alert('Please check your passcode and try again'); return false;"
您将
location.href
设置得很好,但问题是,在浏览器有机会导航到新页面之前,您的表单会被提交(因为用户单击了type=“submit”
按钮)。提交表单会导致浏览器开始对表单操作的新导航,这将取消您通过设置location.href
启动的导航。在您的代码中,表单的操作尚未设置,因此它默认为您已经在的同一页面。实际上,浏览器将导航回同一页面,因此看起来好像什么也没有发生——尽管您可能会注意到表单的内容已出现在浏览器的导航栏中
您需要防止浏览器在单击按钮时自动提交表单。一种方法是添加returnfalse
到您的onclick
操作的末尾:
onclick="if (document.getElementById('password').value == '12345678') location.href = 'http://example.url.here'; else alert('Please check your passcode and try again'); return false;"
您将
location.href
设置得很好,但问题是,在浏览器有机会导航到新页面之前,您的表单会被提交(因为用户单击了type=“submit”
按钮)。提交表单会导致浏览器开始对表单操作的新导航,这将取消您通过设置location.href
启动的导航。在您的代码中,表单的操作尚未设置,因此它默认为您已经在的同一页面。实际上,浏览器将导航回同一页面,因此看起来好像什么也没有发生——尽管您可能会注意到表单的内容已出现在浏览器的导航栏中
您需要防止浏览器在单击按钮时自动提交表单。一种方法是添加returnfalse
到您的onclick
操作的末尾:
onclick="if (document.getElementById('password').value == '12345678') location.href = 'http://example.url.here'; else alert('Please check your passcode and try again'); return false;"
您将
location.href
设置得很好,但问题是,在浏览器有机会导航到新页面之前,您的表单会被提交(因为用户单击了type=“submit”
按钮)。提交表单会导致浏览器开始对表单操作的新导航,这将取消您通过设置location.href
启动的导航。在您的代码中,表单的操作尚未设置,因此它默认为您已经在的同一页面。实际上,浏览器将导航回同一页面,因此看起来好像什么也没有发生——尽管您可能会注意到表单的内容已出现在浏览器的导航栏中
您需要防止浏览器在单击按钮时自动提交表单。一种方法是添加returnfalse
到您的onclick
操作的末尾:
onclick="if (document.getElementById('password').value == '12345678') location.href = 'http://example.url.here'; else alert('Please check your passcode and try again'); return false;"
您的代码应该如下所示:
<script type="text/javascript">
function onSubmit() {
if (document.getElementById('password').value == '12345678') {window.location.href = 'http://google.co.in'; }else{ alert('Please check your passcode and try again');}
}
</script>
<fieldset><form class="hero-form" action="#">
<div class="row-fluid">
<label>Enter your passcode</label>
<input type="password" name="password" maxlength="8" class="span7" id="password" placeholder="e.g. 12345678" required/>
</div>
</form></fieldset>
<button class="btn btn-info btn-large span5" id="joe_btn" onclick="onSubmit()">Enter</button>
函数onSubmit(){
if(document.getElementById('password')。value=='12345678'){window.location.href='10http://google.co.in“;}其他{alert('请检查您的密码,然后重试');}
}
输入您的密码
进入
你的错误:按钮不应该提交表单,所以我把它带到了表单之外。我已经将location.href更新为window.location.href
希望它能工作。您的代码应该是这样的:
<script type="text/javascript">
function onSubmit() {
if (document.getElementById('password').value == '12345678') {window.location.href = 'http://google.co.in'; }else{ alert('Please check your passcode and try again');}
}
</script>
<fieldset><form class="hero-form" action="#">
<div class="row-fluid">
<label>Enter your passcode</label>
<input type="password" name="password" maxlength="8" class="span7" id="password" placeholder="e.g. 12345678" required/>
</div>
</form></fieldset>
<button class="btn btn-info btn-large span5" id="joe_btn" onclick="onSubmit()">Enter</button>
函数onSubmit(){
if(document.getElementById('password')。value=='12345678'){window.location.href='10http://google.co.in“;}其他{alert('请检查您的密码,然后重试');}
}
输入您的密码
进入
你的错误:按钮不应该提交表单,所以我把它带到了表单之外。我已经将location.href更新为window.location.href
希望它能工作。您的代码应该是这样的:
<script type="text/javascript">
function onSubmit() {
if (document.getElementById('password').value == '12345678') {window.location.href = 'http://google.co.in'; }else{ alert('Please check your passcode and try again');}
}
</script>
<fieldset><form class="hero-form" action="#">
<div class="row-fluid">
<label>Enter your passcode</label>
<input type="password" name="password" maxlength="8" class="span7" id="password" placeholder="e.g. 12345678" required/>
</div>
</form></fieldset>
<button class="btn btn-info btn-large span5" id="joe_btn" onclick="onSubmit()">Enter</button>
函数onSubmit(){
if(document.getElementById('password')。value=='12345678'){window.location.href='10http://google.co.in“;}其他{alert('请检查您的密码,然后重试');}
}
输入您的密码
进入
你的错误:按钮不应该提交表单,所以我把它带到了表单之外。我已经将location.href更新为window.locat