Drop List Trigger Event

I want to trigger a dropdown list change event in $ (document) .ready using jquery.

I have a cascading drop-down menu for the country and state on the user details page. how can I set the value (which is taken from the database based on the user ID) for the country and state in MVC with C #.

+72
jquery asp.net-mvc triggers
May 23 '09 at 18:59
source share
5 answers

I don’t know that there is a lot of jQuery, but I heard that it allows you to fire your own events using this syntax.

$(document).ready(function(){ $('#countrylist').change(function(e){ // Your event handler }); // And now fire change event when the DOM is ready $('#countrylist').trigger('change'); }); 

More details here .

+153
May 24 '09 at 15:18
source share

Try the following:

 $(document).ready(function(event) { $('#countrylist').change(function(e){ // put code here }).change(); }); 

Identify the change event and run it immediately. This ensures that the event handler is defined before it is called.

It may be too late for you to respond to the original poster, but someone might benefit from the abbreviated notation, and that follows the jQuery chain, etc.

jquery chain

+9
Apr 30 '13 at 19:07
source share

If you are trying to link drop-down lists, the best way to do this is to have a script that returns a pre-selected checkbox and an AJAX call that requests it.

Here's the jQuery Ajax documentation if you need it.

 $(document).ready(function(){ $('#countrylist').change(function(e){ $this = $(e.target); $.ajax({ type: "POST", url: "scriptname.asp", // Don't know asp/asp.net at all so you will have to do this bit data: { country: $this.val() }, success:function(data){ $('#stateBoxHook').html(data); } }); }); }); 

Then you have a range around your state select box with the identifier "stateBoxHook"

+2
May 23 '09 at 19:01
source share

Try the following:

$('#id').change();

It works for me.

On one line, along with setting the value: $('#id').val(16).change();

+1
Nov 18 '14 at
source share

alternatively you can put the onchange attribute in the drop-down list that onchange will call a specific jquery function like this.

 <input type="dropdownlist" onchange="jqueryFunc()"> <script type="text/javascript"> $(function(){ jqueryFunc(){ //something goes here } }); </script> 

I hope this helps you, and please note that this code is just a draft, not tested on any idea. thank

+1
Jan 22 '15 at 8:01
source share



All Articles