jquery操作select时怎么产生事件
jquery操作select时怎么产生事件
如我设置2为选中项
$('#select').val(“2”);
哪么怎么同时产生一个onchange事件呢?
谢谢!
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3/jquery.min.js" type="text/javascript"></script>
<title></title>
</head>
<body>
<select id="test">
<option value="1" selected="selected">test</option>
<option value="2">test2</option>
</select>
<script type="text/javascript">
<!--
$.fn.selVal = function(v){
if($(this).val() != v){
$(this).val(v).trigger("change");
}
}
$("#test").change(function(){
alert(this.value);
});
$("#test").selVal(2);
//-->
</script>
</body>
</html>