问题描述:

I want to submit a form using ajax in the background. I tried:

<div class="form-horizontal" id="form">

<label for="name" class="control-label">Username</label>

<div class="controls">

<span id="name_input"><input type="text" name="name" id="medium" class='input-medium input-square'></span>

<span class="help-inline" id = "ajax_load"></span>

</div>

<div class="form-actions">

<button class="btn btn-red5" onclick="resolve()">Login</button>

<input type="reset" class='btn btn-danger' value="Reset">

</div>

</div>

And the Javascript:

<script type="text/javascript">

var resolve = function () {

jAlert('test', 'test');

$('#ajax_load').html('<a href="#" class="btn btn-mini btn-square tip" title="Reloading"><img src="templates/img/ajax-loader.gif" alt=""></a>');

$.ajax( {

url : 'plugin.php?plugin=test',

type : 'post',

data: $("#form").serialize(),

success : function( resp ) {

if(resp.ajax_success == false) {

} else {

jAlert('test', 'test');

}

}

});

};

</script>

I get an alert, but there is no form submit. I checked that with Live http headers.

Why does it not submit the form?

网友答案:

If it doesn't submit the form, because, the #form is not a form.

Change:

<div class="form-horizontal" id="form">

To:

<form class="form-horizontal" id="form">

You need to replace the resp.ajax_success with resp. Let us also know the response of the plugin.php?plugin=test URL.


If you don't want the <form> to get submitted on clicking on the Submit button, add a return false; at the end of the resolve function this way:

var resolve = function () {
    jAlert('test', 'test');
    $('#ajax_load').html('<a href="#" class="btn btn-mini btn-square tip" title="Reloading"><img src="templates/img/ajax-loader.gif" alt=""></a>');
    $.ajax( {
        url: 'plugin.php?plugin=test',
        type: 'post',          
        data: $("#form").serialize(),
        success: function( resp ) {
            if(resp.ajax_success == false) {

            } else {
                jAlert('test', 'test');
            }
        }
    });
    return false;
};
网友答案:

its because you haven't used the form you are serializing the data from div

<div class="form-horizontal" id="form">

not form the form

should be

<form class="form-horizontal" id="form">
相关阅读:
Top