执行jquery .submit在ajax成功后返回新表单

执行jquery .submit在ajax成功后返回新表单

问题描述:

I believe my problem has something to do with the fact that my first form RETURNS a new form via ajax success .html(result) AFTER DOM has executed. My jquery within DOM isn't being recognized because elements aren't visible until after the submit of first form. HOW to get my $("#fullFormMA").on(submit,(function(e){ to execute is eluding me. Here is my html

<?php 
session_start();
require_once('functions.php');
include('header.htm');?>
<title>Membership Application</title>
<meta name="description" content="">
</head> 
<body> 
<div id="container">
    <div id="loginBanner">
        <?php include ("loginMenu.php"); ?>
        <?php include ("bannerIcons.php"); ?>
    </div> <!--end loginBanner-->
    <div id="header" class="clear">
        </div> <!--end header-->
    <div id="content"><div class="content">
        <div id="colLt">
            <?php include('tabContent.php');?>
            <?php include('leftSidebar.php');?>
        </div>
        <div id="colRt"><div class="content">
        <h1>New Member Application</h1>
        <ul><li>submitting an application</li><li>submitting payment</li></ul><h6>Step #1&mdash;the application</h6>Please enter an email which will ultimately be used as your website username.  This email will remain as your private email.</p><br><br>
        <form method="post" name="checkUserMA" id="checkUserMA">
            <label class="clear" style="width:120px">Username/Email<br><span class="small"></span></label>
            <input type="text" name="usernameMA" id="usernameMA" class="green" style="width:300px;"/><br><br>
            <input type="submit" id="checkUserMA" class="submit" value="Submit" />
        </form>
        <div class="clear"></div>
        <div id="errorMA" style="background:yellow;width:200px;height:100px"></div>
        <div id="resultMA"></div>       
    </div></div>
    <div class="clear"></div>
    </div></div><!--end content-->  
<div id="footer">
<?php include("footer.htm") ?>
<!--<?php include("disclaimer.htm") ?>-->
</div><!--end footer-->
<div class="clear"></div>
</div><!--end container-->
<div class="clear"></div>
</body> 
</html> 

Here is my jquery:

$(document).ready(function() {

$('#resultMA').hide();
$('#errorMA').hide();
$("#checkUserMA").submit(function(event){
    event.preventDefault();
    $("#resultMA").html('');
    var values = $(this).serialize();
    $.ajax({
        url: "checkMA.php",
        type: "post",
        data: values,
        success: function(result){
            $("#resultMA").html(result).fadeIn();
            $('.error').hide();
        },
        error:function(){
           // alert("failure");
            $("#resultMA").html('There was an error.  Please try again.').fadeIn();
        }
    });//end ajax
});

$("#fullFormMA").on(submit,(function(e){
    e.preventDefault();
    $("#errorMA").html('');
    var values = $(this).serialize();
    $.ajax({
        url: "validMA.php",
        type: "post",
        data: values,
        success: function(result){
        },
        error:function(){
        // alert("failure");
            $("#errorMA").html('There was an error.  Please try again.').fadeIn();
        }
    });//end ajax
    });
});//end dom

Here is checkMA.php...

<?php
session_start();
include('functions.php');
connect();
$username = urldecode(protect($_POST['usernameMA']));
$_SESSION['guestUser'] = $username;
$sql2 = mysql_query("SELECT username FROM members WHERE username = '$username'");
$checkNumRows = mysql_num_rows($sql2);
if (!$username){
    echo "<p class='red'>Enter an email to be used as your username...</p>";
} else if ($checkNumRows == 1){
    echo "<span style='font-weight:bold'>The username: ".$username." is already in use.</span>";    
} else if ($checkNumRows == 0){
    echo "<hr><p class='green'>This username is available.</p><p>Please continue with the registration process...</p><br>";?>
    <form method="post" name="fullFormMA" action="memberAppProcess.php">
        <h6>Public Information - this information will be displayed to website visitors</h6>
        <label class="clear" style="width:75px">Name</label>
        <label class="error" id="name_error">This field is required.</label>
        <input type="text" name="firstName" id="firstName" class="left inputCheck" style="width:150px" placeholder="first name"/>
        <input type="text" name="lastName" id="lastName" class="inputCheck" style="margin-left:10px" placeholder="last name"/><br><br>
        <input type="submit" name="fullFormMA" id="fullFormMA" class='submit right' onClick='submitFullForm();' value="Submit application">
        </form> 
    <?php
}?>

My #checkUserMA works but my #fullFormMA doesn't work. I would love to understand why (DOM already loaded?) and how I might fix my code to allow for a form added "after the fact" via ajax .html(result). Thank you.

The DOM is ready before your ajax success so you can write this JQuery full code

   $(document).ready(function() {
   $('#resultMA').hide();
   $('#errorMA').hide();
   $("#checkUserMA").submit(function(event){
event.preventDefault();
$("#resultMA").html('');
var values = $(this).serialize();
$.ajax({
    url: "checkMA.php",
    type: "post",
    data: values,
    success: function(result){
        $("#resultMA").html(result).fadeIn();
        $('.error').hide();
        RunAfterAjax();
       },
    error:function(){
       // alert("failure");
        $("#resultMA").html('There was an error.  Please try again.').fadeIn();
    }
});//end ajax
});
 function RunAfterAjax(){


 $("#fullFormMA").on(submit,(function(e){
e.preventDefault();
$("#errorMA").html('');
var values = $(this).serialize();
$.ajax({
    url: "validMA.php",
    type: "post",
    data: values,
    success: function(result){
    },
    error:function(){
    // alert("failure");
        $("#errorMA").html('There was an error.  Please try again.').fadeIn();
    }
});//end ajax
});
}
});//end dom

It's executing, you just aren't waiting long enough for it to exist. Move the event binding for the new form to the line right after you add the new form to the document.

$("#resultMA").html(result).fadeIn();
$("#fullFormMA").on(submit,(function(e){...

$("#fullFormMA").on(submit,(function(e){ /* ... */ });

fullFormMA is an <input>, you should bind click instead of submit, and use quotes around the event name.

When you use $('#something').on('event', ...), it only works if the #something element already exists.

You could fix your code by delegating the listener to an upper existing element :

$('#content').on('click', '#fullFormMA', function() { /* ... */ });

This code will detect the click event on #fullFormMA event if it is added after an ajax response.