Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

simulating an change event jquery

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?

like image 488
Raphaël Avatar asked Nov 10 '11 10:11

Raphaël


2 Answers

try .trigger()

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

It can also be chained into a single statement:

$('#yourselect').val('yourvalue').trigger('change');
like image 194
Andy Avatar answered Nov 12 '22 19:11

Andy


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).

like image 34
roselan Avatar answered Nov 12 '22 19:11

roselan