gpt4 book ai didi

jQuery 表单验证不起作用

转载 作者:行者123 更新时间:2023-12-01 08:25:37 24 4
gpt4 key购买 nike

我在这里为表单验证而苦苦挣扎:

// JavaScript Document
$(document).ready( function() {

$("#submit").click(function(){

// set initial error value to false
var hasError = false;

// set var for each form field
var order_ref = $("#order_ref").val();
var order_name = $("#order_name").val();
var order_telephone = $("#order_telephone").val();
var order_email = $("#order_email").val();
var order_message = $("#order_message").val();
var order_size = $("#sendEmail input.radio:checked").val();

// validate each of them
if(order_ref == '') { $("#order_ref").addClass('haserror'); hasError = true; }
if(order_name == '') { $("#order_name").addClass('haserror'); hasError = true; }
if(order_telephone == '') { $("#order_telephone").addClass('haserror'); hasError = true; }
if(order_email == '') { $("#order_email").val().addClass('haserror'); hasError = true; }
//if(order_message == '') { $("#order_message").val().addClass('haserror'); hasError = true; }
if(order_size == '') { $("#order_size").val().addClass('haserror'); hasError = true; }

// if there are errors
if(hasError == true) {
alert('whoa!');
return false;
};

// if there's no errors, proceed
if(hasError == false) {
$.post("/photo/theme/foodphoto/includes/mail_send.php",
{
// pass each of the form values to the PHP file for processing
order_ref: order_ref,
order_name: order_name,
order_telephone: order_telephone,
order_email: order_email,
order_message: order_message,
order_size: order_size
},
function(data){
// no errors? great now do what you want to show the user his message is sent
$('#sendEmail, .faded').slideUp(500);
$('.thanks').delay(500).fadeIn(500);
}
);
};

return false;
});

});

你能发现我做错了什么吗?

谢谢! :)

最佳答案

这些行是错误的:

if(order_email == '') { $("#order_email").val().addClass('haserror'); hasError = true; } 
if(order_size == '') { $("#order_size").val().addClass('haserror'); hasError = true; }

删除 val()

复制/粘贴错误,看起来像:)

关于jQuery 表单验证不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4212937/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com