SitePoint Sponsor

User Tag List

Results 1 to 3 of 3

Hybrid View

  1. #1
    SitePoint Member
    Join Date
    Jan 2007
    Posts
    12
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    How to trigger a form submit after a function has completed

    I have a php form and am using jquery to edit the name attribute before the form is submitted. I am passing a variable in the class attribute and then adding that to the name attribute so I can use it again when the form is processed. I'm using the following code to accomplish this. The change to the name attribute is triggered when the submit button is pushed. Once the function is completed I want the form to be submitted without the user needing to take any other action. I've tried a number of different ways and have not been able to get the form to submit. Any suggestions?

    Code JavaScript:
    $('input[name=csvsubmit]').click(function(event) {
    		event.preventDefault();
              $('input').each(function() {
              	var eachClass = $(this).attr('class');
    			var eachName = $(this).attr('name');
    			$(this).attr('name', eachName+'_'+eachClass);
              });
              $('input[name=csvsubmit]').trigger('submit');
    	});

  2. #2
    SitePoint Enthusiast aufshebung's Avatar
    Join Date
    May 2012
    Location
    Santa Fe, NM
    Posts
    22
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Just remove the "event.preventDefault()" line. Your function will be executed before the submit takes place, and you want the submission to happen anyway, so it's unnecessary.

    Why didn't the trigger work?

    I think it's because you were trying to access an element that no longer existed. You looped through each <input> element and added an underscore and that element's classes to its name. And your submit button is an <input> element, so even if it didn't have any classes, its name was still changed to "csvsubmit_".

  3. #3
    SitePoint Member
    Join Date
    Jan 2007
    Posts
    12
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks, removing the event.preventDefault() worked.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •