我有以下HTML代码:
<form class="form" method="post">
<h2 id="formheadline" class="options-headline">Change password</h2>
<div class="form-row">
<label class="form-label">
Current password
</label>
<input type="password" name="password" id="old" required = "required" class="form-input"
placeholder="Current Password"/>
<span id="old-password-error" class="error-text"></span>
</div>
<div class="form-row">
<label class="form-label">
New Password
</label>
<input type="password" name="password" id="new" placeholder="New Password" required = "required"
class="form-input" minlength="8" maxlength="12"
pattern="(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{8,12}"/>
</div>
<div class="form-row">
<label class="form-label">
New password
</label>
<input type="password" name="password" id="repeat" placeholder="New password"
required = "required" class="form-input" minlength="8" maxlength="12"
pattern="(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{8,12}"/>
<span id="new-password-error" class="error-text"></span>
</div>
<div class="form-row form-row-center">
<button type = "reset" id="save-changes-button" class="button button-submit">
Save
</button>
</div>
</form>
我需要禁用该按钮,直到所有字段都不为空。
我尝试使用form.js
文件中的以下行解决它:
$(function(){
$('#save-changes-button').button("disabled");
$("input").on('change keyup',isFormValid);
});
function isFormValid(){
var formIsValid = isOldPasswordValid()&&isConferemedPasswordValid&&isNewPasswordValid;
$("buttom[type='reset']").prop("disabled", !formIsValid);
}
function isOldPasswordValid(){
var oldPasswordInput = $("#old").val();
if (oldPasswordInput.length == 0){
return false;
}
return true;
}
function isNewPasswordValid(){
var newPasswordInput = $("#new").val();
if (newPasswordInput.length == 0){
return false;
}
if (newPasswordInput.length<4){
return false;
}
return true;
}
function isConferemedPasswordValid(){
var conferemedPasswordInput = $("#repeat").val();
var newPasswordInput = $("#new").val();
if (confermedPasswordInput != newPasswordInput){
return false;
}
return true;
}
但我跑步时却没有反应。该按钮仍然保持启用状态。
我也不确定我是否正确连接2个文件,是否足以写<script type="text/javascript" src="form.js"/>
或者还有更多我忘了
答案 0 :(得分:0)
为什么要在第一行查询函数?你可以直接打电话。
$('#save-changes-button').button("disabled");
$("input").on('change keyup',isFormValid);
function isFormValid(){
var formIsValid = isOldPasswordValid()&&isConferemedPasswordValid&&isNewPasswordValid;
$("button[type='reset']").prop("disabled", !formIsValid);
}
function isOldPasswordValid(){
var oldPasswordInput = $("#old").val();
if (oldPasswordInput.length == 0){
return false;
}
return true;
}
function isNewPasswordValid(){
var newPasswordInput = $("#new").val();
if (newPasswordInput.length == 0){
return false;
}
if (newPasswordInput.length<4){
return false;
}
return true;
}
function isConferemedPasswordValid(){
var conferemedPasswordInput = $("#repeat").val();
var newPasswordInput = $("#new").val();
if (confermedPasswordInput != newPasswordInput){
return false;
}
return true;
}
答案 1 :(得分:0)
$("button[type='reset']").prop("disabled",true);
$("input").on('input',isFormValid);
function isFormValid(){
var formIsValid = isOldPasswordValid()&&isConferemedPasswordValid()&&isNewPasswordValid();
$("button[type='reset']").prop("disabled", !formIsValid);
}
function isOldPasswordValid(){
var oldPasswordInput = $("#old").val();
if (oldPasswordInput.length == 0){
return false;
}
return true;
}
function isNewPasswordValid(){
var newPasswordInput = $("#new").val();
if (newPasswordInput.length == 0){
return false;
}
if (newPasswordInput.length<4){
return false;
}
return true;
}
function isConferemedPasswordValid(){
var conferemedPasswordInput = $("#repeat").val();
var newPasswordInput = $("#new").val();
if (conferemedPasswordInput != newPasswordInput){
return false;
}
return true;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form class="form" method="post">
<h2 id="formheadline" class="options-headline">Change password</h2>
<div class="form-row">
<label class="form-label">
Current password
</label>
<input type="password" name="password" id="old" required = "required" class="form-input"
placeholder="Current Password"/>
<span id="old-password-error" class="error-text"></span>
</div>
<div class="form-row">
<label class="form-label">
New Password
</label>
<input type="password" name="password" id="new" placeholder="New Password" required = "required"
class="form-input" minlength="8" maxlength="12"
pattern="(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{8,12}"/>
</div>
<div class="form-row">
<label class="form-label">
New password
</label>
<input type="password" name="password" id="repeat" placeholder="New password"
required = "required" class="form-input" minlength="8" maxlength="12"
pattern="(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{8,12}"/>
<span id="new-password-error" class="error-text"></span>
</div>
<div class="form-row form-row-center">
<button type="reset" id="save-changes-button" class="button button-submit" disabled>
Save
</button>
</div>
</form>
答案 2 :(得分:0)
如何更改您正在使用的功能,使用更通用的功能(在表单中添加id,以便我们可以在这里使用)
<form class="form" method="post" id="change-password">
function isFormValid(){
var formInvalid = false
$('#change-password input').each(function() {
if ($(this).val() === '') {
formInvalid = true
return false;
}
$("button[type='reset']").prop("disabled", formInvalid);
});
}