本文实例讲述了jquery trigger实现联动的方法。分享给大家供大家参考,具体如下:
|
<html> <head> <title>testing</title> <script src= "jquery-1.3.2.min.js" type= "text/javascript" ></script> <script type= "text/javascript" > $( function () { $( "#Provinces" ).change( function (e,cityValue) { if ($( this ).val() == "1" ) { $( "#City" ).html( "<option value='1' >长沙</option><option value='2' >衡阳</option>" ); } else { $( "#City" ).html( "<option value='1' >武汉</option><option value='2' >襄阳</option>" ); } if ( typeof (cityValue) != "undefined" ) { $( "#City" ).val(cityValue); } }); }); function test() { $( "#Provinces" ).val( "2" ); $( "#Provinces" ).trigger( "change" , "2" ); } </script> </head> <body> 省: <select id= "Provinces" > <option value= "1" >湖南</option> <option value= "2" >湖北</option> </select><br /> 市: <select id= "City" > <option value= "1" >长沙</option> <option value= "2" >衡阳</option> </select><br /> <input type= "button" value= "设置成湖北襄阳" onclick= "test()" /> </body> </html> |
众所周知, trigger 是异步的, 在其后面执行的代码可能会跑到它前面去执行, 上面的代码就避免了这一点。