jQuery onclick does not work

前端 未结 4 624
梦如初夏
梦如初夏 2021-01-23 12:25

I trying to get onclick work, but it does not... Here is my code:

HTML




        
4条回答
  •  臣服心动
    2021-01-23 12:52

    Your JS is being exucuted before the DOM elements have loaded. Your event handler is therefore not being attached to the element since it doesn't exist at the time.

    Wrap your JS with a DOM ready handler:

    $(document).ready(function () {
        $('#submit').click(function(){
            alert('This onclick function forks!');
        });
    });
    

    You could also just use event delegation since the document object exists at the time of execution:

    $(document).on('click', '#submit', function () {
        alert('This onclick function forks!');
    });
    

提交回复
热议问题