我试图在单击功能上使用Java脚本来验证表单,即在表单提交过程中,并且如果条件被删除,我会收到警报消息,但是警报消息应该显示在if条件下,即当表单为空时
$(document).ready(function(){
$('#submit').click(function(){
var name = $().val("#name");
var email = $().val("#email");
if(name == '' || email == ''){
alert("test");
}
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<form>
<input id="name" type="text" placeholder="name">
<input id="email" type="email" placeholder="email">
<button id="submit" type="submit">submit</button>
</form>
答案 0 :(得分:1)
In jQuery, input values are retrieved with $(element).val()
. Your variables should look like this:
var name = $('#name').val();
var email = $('#email').val();
答案 1 :(得分:0)
$(document).ready(function(){
$('#submit').click(function(){
var name = $("#name").val();
var email = $("#email").val();
if(name == '' || email == ''){
alert("test");
return false;
}
});
});
答案 2 :(得分:0)
You are not selecting the input elements you want to get the values from. Replace
var name = $().val("#name");
var email = $().val("#email");
with
var name = $("#name").val();
var email = $("#email").val();