2

my code is as follows

$(document).ready(function() {

    // this is a button to add the form to the DOM tree.
    $("#submitPara").click(function() {
        $("body").append('<form id = "dimensionAndObjects" action = "#"></form>');
        some code to add some input fields, omitted...
        $('#dimensionAndObjects').append('<p><input id = "submitDO" type = "submit" value = "submit"></input></p>');
        return true;
    });

    $('#dimensionAndObjects').submit(function() {
        alert("haahhhahhaha");
        return false;
    });

});

it seems that the submit function doesn't work because the alert info doesn't appear. if i put the submit function inside the click function, it doesn't work either. what's wrong? I am a totally freshman to jQuery, thanks in Advance!

0

2 Answers 2

4

since the form is created dynamically you need to use event delegation

$(document).on('submit', '#dimensionAndObjects', function() {
        alert("haahhhahhaha");
        return false;
    });
Sign up to request clarification or add additional context in comments.

2 Comments

+1 for seeing the need to attach to document, with the way the code is structured.
if we passed a handler to the submit function instead of performing the submit it would attach the handler to the submit event. Thats why the submit didn't happen right?
0
$('#dimensionAndObjects').live('click',function(e) {
e.preventdefault();
        alert("haahhhahhaha");
        return false;// for not submit the form
return true ;// for submit the form
    });

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.