0

I am having a bear of a time figuring out why a jQuery .submit() is not recognizing the submit.

The HTML form (actually multiple forms) are successfully generated into a div that is appended to the page with the following javascript function (this is called out of $(document).ready(function(){}):

function makeMenu(chambers,bins,values) {
    var htmlString = "";
    for(var i = 0; i < chambers.length; i++) {
        if(bins[i]) {
            var link = chambers[i] + " - " + bins[i];
            var p = values[i];
        }

        htmlString += "<form name='entity' id='entity' method='post' action=''>";
        htmlString += "<input type='submit' name='get_entity' id='to_php' value='" + link + "' class='text_button' title='value=" + p + "'>";
        htmlString += "<input type='hidden' name='tool' value='" + chambers[i] + "'/>";
        htmlString += "<input type = 'hidden' name='bin' value='" + bins[i] + "'/>";
        htmlString += "</form>";
    }

    $("#spacer").append("<div class='menu'>" + htmlString + "</div>");
}

Upon submitting a form I am just trying to get an alert to post to show that the submit is recognized but it does not get there:

$(document).ready(function() {
    $('#entity').submit(function() {
        alert('Success!');
    });
 })

I have tried making sure that the submit button did not have submit as the name and id and tried just generating a single form but no luck. Any assistance is much appreciated.

3
  • When $('#entity').submit() is executed, do you think $('#entity') exists? Commented Dec 18, 2013 at 17:31
  • 1
    Also, element ids must be unique. Use a class instead. Commented Dec 18, 2013 at 17:33
  • I've run into another problem. The makeMenu is creating multiple forms of class=entity. When one of them is clicked on I want to pass the chamber and bin information to php using serialize. Unfortunately, the serialized string contains the information for ALL chamber/bin combinations. I need it to only include information for the link clicked. Is there a way to do this? Commented Dec 18, 2013 at 19:15

2 Answers 2

3

Try

$(document).ready(function() {
    $(document).on('submit','#entity',function() {
        alert('Success!');
    });
 })
Sign up to request clarification or add additional context in comments.

6 Comments

Responding to Alexandre as he had the first solution. Works great now. I'm only a couple months into javascript/jquery, learn something new every day. Also, per Jason I did change it to a class instead of id but it worked both ways. Thanks for the quick responses!
I've run into another problem. The makeMenu is creating multiple forms of class=entity. When one of them is clicked on I want to pass the chamber and bin information to php using serialize. Unfortunately, the serialized string contains the information for ALL chamber/bin combinations. I need it to only include information for the link clicked. Is there a way to do this?
You are using jquery .serialize() ?
Yes. My ajax is per below
$.ajax({ type:'POST', data: $('.entity').serialize(), url: 'get_prob.php', dataType: 'json', success: function(data) { alert('Success'); } });
|
0

Try on instead of submit. As the form is created by jQuery it isn't bound to the document yet.

$(document).ready(function() {
    $(document).on('submit','#entity',function() {
        alert("success");
    });
 })

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.