I'm writing a form that will change all of the user's profile settings. I have the HTML and PHP set up correctly and working, but using jQuery conditionals & Ajax is proving to be a problem.
It should validate for an email if one is there. Then, it should make sure the change isn't a password change (if it is, make sure passwords match then post), then post if the form was valid. It seems to not consider any of the conditionals and post regardless.
$("profSet").validate({
rules: {
chem: {
email: true
}
}
});
$('#subSet').live('click',function() {
if($("#chfn").val() == 0 && $("#chln").val() == 0 && $("#chpw").val() == 0 && $("#chpw2").val() == 0 && $("#chem").val() == 0)
{
if($("#profSet").valid())
{
if($("#chpw").val() == "" && $("#chpw2").val() == "")
{
$.post('php/profSet.php', $('#profSet').serialize(), function(){
$('#profSet').hide();
$('#success').show();
});
}
else
{
if($("#chpw").val() == $("#chpw2").val())
{
$.post('php/profSet.php', $('#profSet').serialize(), function(){
$('#profSet').hide();
$('#success').show();
});
}
else
{
$('#error').show();
}
}
}
}
开发者_运维知识库 });
Any help?
You may want to declare a var valid = true
at the beginning of your click method and set it to false if any of your checks fail, then at the end of your validation methods, perform the check if(!valid) { e.preventDefault() }
to stop event propagation. Your handler does nothing to stop event propagation right now. In order to accomplish that, you should change your anonymous click handler function to accept the event as a parameter which I called "e" so that you can short-circuit the submission.
精彩评论