チェックボックスがオフでテキストボックスが空の場合、検証メッセージをポップアップする必要があります。ポップアップは表示されますが、入力されたテキストは削除され、無効のままになります。最初の文字が入力されたときに検証が消え、テキストボックスが空白でない限り再表示されないようにするにはどうすればよいですか?
<form id="practiceForm">
<input type="text" name="firstName" id="textbox"/>
<input type="checkbox" name="active" id="checkbox"/>
<input type="submit" id="submit" value="Submit"/>
</form>
<script type="text/javascript">
$('#checkbox').attr('checked', true);
if($('#checkbox').attr('checked')){
$('#textbox').attr('disabled', true);
$('#textbox').val('');
} else {
$('#textbox').attr('disabled', false);
};
$('#checkbox').change(function () {
if($('#checkbox').attr('checked')){
$('#textbox').attr('disabled', true);
$('#textbox').val('');
} else {
$('#textbox').attr('disabled', false);
};
});
$.validator.addMethod("textValidate", function(value){
if(!$('#checkbox').attr('checked')){
if(!$('#textbox').val('') || !$('#textbox').val(null)){
return true;
};
};
}, "If box is not checked then there must be text"
);
$('#practiceForm').validate({
rules: {
//textValidate: true
firstName:{
textValidate: true
}
}
});
</script>