2

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

1
  • I just put together a test with that exact code and it worked for me (in Chrome anyway). I hope someone answers this question, because I'm interested to find out why it didn't work for you. Commented Nov 4, 2010 at 4:20

2 Answers 2

2

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.
Sign up to request clarification or add additional context in comments.

1 Comment

SO thankful Craver. the problem was solved with document ready.
0

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.

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.