10

I have five lists (select) that are populated with php / mysql and jquery. I use this script: http://bit.ly/3YTAXe

When I post my form and the new page appears, I redisplay my five list and I want my 5 lists will populate with the values ​​posted.

I think I'd have to simulate the event "change" or something like that. I do not think my code can help (it's inside the smarty and many other things).

Does anyone can help me from the example code that is in the link above?

Raphaël
  • 1,141
  • 3
  • 18
  • 32
  • 7
    have you tried `$('#yourselect').trigger('change');` ? – Andy Nov 10 '11 at 10:05
  • How can I do to simulate the event by choosing the right value? Because it is based on the value that my second list will be populated. – Raphaël Nov 10 '11 at 10:15

2 Answers2

17

try .trigger()

$('#yourselect').val('yourvalue');
$('#yourselect').trigger('change');

It can also be chained into a single statement:

$('#yourselect').val('yourvalue').trigger('change');
E. Villiger
  • 876
  • 10
  • 27
Andy
  • 29,707
  • 9
  • 41
  • 58
1

a standard post action ask the server for a new page, which has no natural "memory" of the the form posted. So you have 3 basics options:

  1. send the values back from your servers
  2. use cookies to store them, and javacript to handle the cookies
  3. use jquery $.post instead of the standard post.

I prefer the 3rd option. jquery will send the form in ajax, and handle the response from the server inside the actual page. Of course, you have to "event.preventDefault" on the form submit button click (or in $('form').submit). Personally, I don't define any "action=" tag in forms supposed to be sent by $.post (i use a variable inside the $.post block).

roselan
  • 3,755
  • 1
  • 20
  • 20