jquery trigger实现联动的方法
本文实例讲述了jquerytrigger实现联动的方法。分享给大家供大家参考,具体如下:
<html>
<head>
<title>testing</title>
<scriptsrc="jquery-1.3.2.min.js"type="text/javascript"></script>
<scripttype="text/javascript">
$(function(){
$("#Provinces").change(function(e,cityValue){
if($(this).val()=="1"){
$("#City").html("<optionvalue='1'>长沙</option><optionvalue='2'>衡阳</option>");
}else{
$("#City").html("<optionvalue='1'>武汉</option><optionvalue='2'>襄阳</option>");
}
if(typeof(cityValue)!="undefined"){
$("#City").val(cityValue);
}
});
});
functiontest(){
$("#Provinces").val("2");
$("#Provinces").trigger("change","2");
}
</script>
</head>
<body>
省:
<selectid="Provinces">
<optionvalue="1">湖南</option>
<optionvalue="2">湖北</option>
</select><br/>
市:
<selectid="City">
<optionvalue="1">长沙</option>
<optionvalue="2">衡阳</option>
</select><br/>
<inputtype="button"value="设置成湖北襄阳"onclick="test()"/>
</body>
</html>
众所周知,trigger是异步的,在其后面执行的代码可能会跑到它前面去执行,上面的代码就避免了这一点。
更多关于jQuery相关内容感兴趣的读者可查看本站专题:《jQuery拖拽特效与技巧总结》、《jQuery扩展技巧总结》、《jQuery常见经典特效汇总》、《jQuery动画与特效用法总结》、《jquery选择器用法总结》及《jQuery常用插件及用法总结》
希望本文所述对大家jQuery程序设计有所帮助。