[英]jQuery first click addClass, second click redirect
I am trying to build a temporary (and fake) form validation. 我正在尝试建立一个临时的(和伪造的)表单验证。
On the form, if you click submit the first time, it adds a class of ".error" and a span to the required inputs. 在表单上,如果您第一次单击“提交”,它将在所需的输入中添加“ .error”类和跨度。 If you click submit again, I want it to redirect to another page.
如果再次单击提交,我希望它重定向到另一个页面。
I can't seem to figure out how to have two different functions on the same submit button. 我似乎无法弄清楚如何在同一个提交按钮上具有两个不同的功能。 The first click needs to add a class, the second click should redirect.
第一次单击需要添加一个类,第二次单击应重定向。
Here's my code: 这是我的代码:
if($("button").hasClass('redirect')){
$("button").click(function(){
window.location.href="index.html";
});
} else {
$("button").click(function(){
$(this).addClass("redirect");
$("input:required").addClass('error');
$("<small class=error>Invalid entry.</small>").insertAfter("input:required");
});
}
you can do like this: 您可以这样:
$("button").click(function(){
if(!$(this).hasClass('redirect'))
{
$(this).addClass("redirect");
$("input:required").addClass('error');
$("<small class=error>Invalid entry.</small>").insertAfter("input:required");
}
else
{
window.location.href="index.html";
}
});
maybe you should just use the same function? 也许您应该只使用相同的功能? something like that :
像这样的东西:
$("button").click(function(){
if($("button").hasClass('redirect')){
window.location.href="index.html";
} else {
$(this).addClass("redirect");
$("input:required").addClass('error');
$("<small class=error>Invalid entry.</small>").insertAfter("input:required");
});
}
$("button").click(function(){
if($("button").hasClass('redirect')){
window.location.href="index.html";
} else {
$(this).addClass("redirect");
$("input:required").addClass('error');
$("<small class=error>Invalid entry.</small>").insertAfter("input:required");
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.