简体   繁体   中英

Issue on Submitting a Form using jQuery

Can you please take a look at this Demo and let me know why I am not able to submit the Form (No Alert after clicking the Submit Button).

<div class="container">
    <form id="target">
        <div class="btn-group btn-group-xs pull-right" data-toggle="buttons">
            <label class="btn btn-default yesactive active ">
            <input type="radio" name="options" id="option1"><span class="glyphicon glyphicon-ok" />
            </label>
            <label class="btn btn-default noactive">
           <input type="radio" name="options" id="option2"><span class="glyphicon glyphicon-remove" />
            </label>
        </div>
        <button type="button" class="btn btn-default btn-sm" id="submit">Submit</button>
    </form>
</div>

<script>
$( "#target" ).on( "submit", function( e ) {
    alert("Handler for");
    e.preventDefault();
});
</script>

Change

<button type="button" class="btn btn-default btn-sm" id="submit">Submit</button>

To

<button type="submit" class="btn btn-default btn-sm" id="submit">Submit</button>

You are calling the event on "Submit", but in your button the type of the button is not submit. If you change to submit then it will cater for the submit event.

http://jsfiddle.net/s3774/13/

Try this one out. JQuery uses an input of type submit for form submissions.

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