简体   繁体   中英

jQuery submit working after second click only

I wrote this code that should submit registration form (PHP) using jQuery and iframe.

Javascript :

<script type="text/javascript">
    $(document).on('click', '#reg_form_sub', function(event) {

        var error_message_lgn = $("#reg_frame").contents().text();

        $('#reg_error').text(error_message_lgn);

    });
</script>

HTML :

    <iframe name="reg_frame" id="reg_frame" style="display:none;" src="register.php"></iframe>
    <table>
        <tr>
            <td>
                <p id="reg_error"></p>
            </td>
        </tr>
        <tr>
            <td><input type="email" name="reg_email" id="reg_form_input" placeholder="Email"></td>
        </tr>
        <tr>
            <td><input type="password" name="reg_pass" id="reg_form_input" placeholder="Password"></td>
        </tr>
        <tr>
            <td><input type="password" name="reg_pass_confirm" id="reg_form_input" placeholder="Confirm password"></td>
        </tr>
        <tr>
            <td><input type="submit" name="reg_submit" id="reg_form_sub" value="Send me a confirmation email"></td>
        </tr>
    </table>
</form>

It is kind of working, but when I click the button the first time nothing happens.

When I click the second time I get echo from PHP (script submits the form, it creates the account etc, but I get echo only after clicking for the second time).

I just added onload and now its working

$(document).on('click', '#reg_form_sub', function(event) {

    $('#reg_frame').on("load", function() {

      var error_message_lgn = $("#reg_frame").contents().text();

      $('#reg_error').text(error_message_lgn);

    });

  });

First there is no form Tag. If you planning to save data's use button click function.

$("button_id_name").click(function(){});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM