简体   繁体   中英

html5 / jquery - click event not registering for html button tag

I am trying to validate a form, but I can't get any code to execute when my button is clicked. Here's my code:

<div id="vehicle_form">
    <form method="post" name="emailForm" action="">
    <input class="dField" id="dfEmail" type="email" name="email" value="Email" onfocus="clearInput(this);" onblur="restoreInput(this)"><br/>
    <input class="dField" id="dfName" type="text" name="firstname" value="First Name" onfocus="clearInput(this);" onblur="restoreInput(this)"><br/>
    <input class="dField" id="dfLast" type="text" name="lastname" value="Last Name" onfocus="clearInput(this);" onblur="restoreInput(this)"><br/>
    <button type="button" id="dSubmit">Submit</button>
    </form>
</div>

$('#dSubmit').click(function(){
    console.log('click');
});

You need to ensure that your jQuery is wrapped in a <script> tag, and ensure that the function is being loaded, perhaps by a $(document).ready() function, as shown below:

<script type='text/javascript'>
$(document).ready(function(){

    //Your Click event
    $('#dSubmit').click(function(){
        console.log('click');
    });

});
</script>

Working Demo

Actually the code stays wrong. A form can be often submitted by hitting enter inside of a textfield. If you want to make it right you should use the submit event.

<script>
$(function(){
    $('#vehicle_form > form').submit(function(){
        console.log('submit');
    });
});
</script>

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