请帮我解释为什么隐藏功能不起作用
HTML
<form id="form1" method="post">
<b>EmpName</b> <input type="text" id="txtName"/>
<span class="Error" id="Span_Name">Enter Name</span>
<b>Email</b> <input type="text" id="txtEmail"/>
<span class="Error" id="SpanEmail">Email</span>
</form>
的jQuery
$('#form1').submit(function (e) {
e.preventDefault();
var IsOk = Validation();
})
function Validation() {
//e.preventDefault();
var IsVailied = true;
$('.Error').css('background-color', 'black'); //Working
$('.Error').hide();
}
答案 0 :(得分:0)
它应该按预期工作。以下是代码段中的小型演示。
$(document).ready(function() {
$('#button').click(function(){
$('#form1').submit();
});
$('#form1').submit(function (e) {
e.preventDefault();
var IsOk = Validation();
});
Validation = function () {
//e.preventDefault();
var IsVailied = true;
$('.Error').css('background-color', 'black'); //Working
$('.Error').hide();
}
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form id='form1'>
<b>EmpName</b> <input type="text" id="txtName"/>
<span class="Error" id="Span_Name">Enter Name</span>
<b>Email</b> <input type="text" id="txtEmail"/>
<span class="Error" id="SpanEmail">Email</span>
</form>
<button id='button'>Submit</button>
答案 1 :(得分:0)
代码很好。您只需在表单中添加submit
按钮即可执行Validation()
。以下是摘录:
$('#form1').submit(function (e) {
e.preventDefault();
var IsOk = Validation();
})
function Validation() {
//e.preventDefault();
var IsVailied = true;
$('.Error').css('background-color', 'black'); //Working
$('.Error').hide();
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form id="form1" method="post">
<b>EmpName</b> <input type="text" id="txtName"/>
<span class="Error" id="Span_Name">Enter Name</span>
<b>Email</b> <input type="text" id="txtEmail"/>
<span class="Error" id="SpanEmail">Email</span>
<button id='button'>Submit</button> <!-- \\ Put this inside the form-->
</form>
**它按预期工作。 **