walves
walves

Reputation: 2068

Jquery submit dont set values on inputs

i have the this form:

<form id="segform" action="TestServlet" method="POST" >
  <div id="actives" class="rounded-corners">
    <div class="column">                        
     <div class="portlet"><a href="#">link1</a></div>
    </div>
  </div>

  <div id="inactives" class="rounded-corners">
    <div class="column">
     <div class="portlet"><a href="#">link2</a></div>
    </div>
  </div>

  <input type="hidden" name="ffactives" value="foovalue" />
  <input type="hidden" name="ffinactives" value="foovalue" />
  <input type="submit"  />
</form>

I need when the form was submitted the content of links being set into input:

ffactives = "link1" instead of "foovalue" ffinactives = "link2" instead of "foovalue"

i use now the jquery code:

$('#segform').submit(function(){
  var act = $('#actives div.column div.portlet a').text();
  var inact = $('#inactives div.column div.portlet a').text();
  $('input[name=ffactives]').val(act);
  $('input[name=ffinactives]').val(inact);
  return true;
});

But when i receive the inputs the values was not changed. i aspect ffactives = "link1" and ffinactives = "link2" , but i receive ffactives = "foovalue" and ffinactives = "foovalue"

Whats wrong

Upvotes: 2

Views: 1027

Answers (2)

Nick Craver
Nick Craver

Reputation: 630389

What you have works at face value, you can test it here, a few things to check:

  • Is this running in a document.ready handler? e.g. your code should be wrapped in
    $(function() { /* current code */ }); so that $('#segform') doesn't run until the <form> is in the DOM and ready to be found (otherwise it hooks a submit handler up to 0 elements.
  • Are you getting any JavaScript errors? If any occur before your code runs, the submit handler wouldn't be bound, and you'get get default submission behavior...which is what you're seeing.

Upvotes: 2

hybernaut
hybernaut

Reputation: 636

I'm guessing your JS is not running properly. Try this version of your submit handler

$('#segform').submit(function(event){
  var act = $('#actives div.column div.portlet a').text();
  var inact = $('#inactives div.column div.portlet a').text();
  $('input[name=ffactives]').val(act);
  $('input[name=ffinactives]').val(inact);

  console.log('submit:', $('input').val();

  event.preventDefault();
  return false;
});

Then you should see 'submit: link1' in your console when you submit the form.

Upvotes: 0

Related Questions