[英]javascript - form validation - disabled button
我是编程新手,所以如果这是一个愚蠢的问题,请原谅我...
我正在尝试让 javascript(或 jquery - 不确定区别)根据定期表单验证检查切换提交按钮的“禁用”属性。 当我将所有内容都输入表单时它工作正常,但是如果我从第一个密码字段复制并粘贴到第二个密码字段,则不会从按钮中删除禁用属性。
任何帮助将不胜感激...
Javascript...
$(':input').focusin(function(){
$(this).css('background-color','#AABEFF');
}).blur(function(){
$(this).css('background-color','white');
});
function check(){
if (($('#first').val()!="") && ($('#last').val()!="") && ($('#email').val()!="") && ($('#password1').val()!="") &&
($('#password2').val()!="") && ($('#phone1a').val()!="") && ($('#phone1b').val()!="") && ($('#phone1c').val()!="")){
if (($('#password1').val())==($('#password2').val()))
{
$('#submit').removeAttr('disabled');
}
else
{
$('#submit').attr('disabled','disabled');
}
}
else
{
$('#submit').attr('disabled','disabled');
}
}
window.setInterval(check, 200);
形式....
<html>
<head>
<link rel="stylesheet" type="text/css" href="style.css" />
</head>
<body>
<div id="back">
</div>
<div id="wrapper">
<div id="header">
<div id="logout">
<?php
include 'employer_header.php';
?>
</div>
</div>
<?php
include 'employer_menu.php';
?>
<div id="feature">
</div>
<div id="content">
<div class="pad">
<form method="post" action="employer_register_process.php">
<p>First Name: <br>
<input id="first" type="text" name="firstname" size="30"><br></p>
<p>Last Name: <br>
<input id="last" type="text" name="lastname" size="30"><br></p>
<p>Phone 1: <br>
<input id="phone1a" type="text" name="phone1a" style='width:30px' maxlength="3">-
<input id="phone1b" type="text" name="phone1b" style='width:30px' maxlength="3">-
<input id="phone1c" type="text" name="phone1c" style='width:40px' maxlength="4"><br></p>
<p>Phone 2 (Optional): <br>
<input type="text" name="phone2a" style='width:30px' maxlength="3">-
<input type="text" name="phone2b" style='width:30px' maxlength="3">-
<input type="text" name="phone2c" style='width:40px' maxlength="4"><br></p>
<p>email: <br>
<input id="email" type="text" name="email" size="30"><br></p>
<p>password: <br>
<input id="password1" type="password" name="pass1" size="30" mask="x"><br></p>
<p>retype password: <br>
<input id="password2" type="password" name="pass2" size="30" mask="x"><br></p>
<p ><input id='submit' type="submit" name='submit' value='register' disabled='disabled'></p>
</form>
</div>
</div>
</div>
<script type="text/javascript" src="js/jquery.js"></script>
<script type="text/javascript" src="js/register.js"></script>
</body>
</html>
您可以将 onKeyUp 侦听器添加到文本字段,并禁用按钮的禁用属性,或者如果值正确,则启用该属性。
HTML:
<input type="text" onkeyup="validate(this)" />
JavaScript:
function validate(textfield) {
var submitButton = document.getElementById("submit_button");
submitButton.disabled = true;
if(textfield.value == "good") {
submitButton.disabled = false;
}
}
这样的东西应该不错。
哦,JQuery 是一个建立在 Javascript 之上的框架。
你不是在调用你的检查函数。
你应该使用 jquery .keyup() 函数来检查
以下是您的文件的外观示例:
$(':input').focusin(function(){
$(this).css('background-color','#AABEFF');
}).blur(function(){
$(this).css('background-color','white');
});
function checkForm(){
if (($('#first').val()!="") && ($('#last').val()!="") && ($('#email').val()!="") && ($('#password1').val()!="") &&
($('#password2').val()!="") && ($('#phone1a').val()!="") && ($('#phone1b').val()!="") && ($('#phone1c').val()!="")){
if (($('#password1').val())==($('#password2').val()))
{
$('#submit').removeAttr('disabled');
}
else
{
$('#submit').attr('disabled','disabled');
}
}
else
{
$('#submit').attr('disabled','disabled');
}
}
$('#password2').keyup(function(){
checkForm();
});
希望有帮助
我不完全确定您的代码有什么问题,但这绝对有效并且是一种更好的方法:
$(':input').focusin(function(){
$(this).css('background-color','#AABEFF');
}).blur(function(){
$(this).css('background-color','white');
});
$('input').keyup(function(){
if (($('#first').val()!="") && ($('#last').val()!="") && ($('#email').val()!="") && ($('#password1').val()!="") &&
($('#password2').val()!="") && ($('#phone1a').val()!="") && ($('#phone1b').val()!="") && ($('#phone1c').val()!="")) {
if ( $('#password1').val() == $('#password2').val() ) {
$('#submit').removeAttr('disabled');
} else {
$('#submit').attr('disabled','disabled');
}
} else {
$('#submit').attr('disabled','disabled');
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.