JQuery选择下拉更改事件 - 选择默认值

时间:2012-02-06 23:13:09

标签: jquery function if-statement

在选择默认值的同时,我如何仍然允许我的更改事件发生?

默认选择值1,但也可以使用更改功能..

<select id='statsGraphingSelect'>
    <option value='1' selected="selected">1</option>
    <option value='2'>2</option>
    <option value='3'>3</option>
    <option value='4'>4</option>
</select>       

$("#statsGraphingSelect").change(function() {
    if ($("#statsGraphingSelect option[value='1']").attr('selected')) {//something happens here }
    if ($("#statsGraphingSelect option[value='2']").attr('selected')) {//something happens here }
    if ($("#statsGraphingSelect option[value='3']").attr('selected')) {//something happens here }
    if ($("#statsGraphingSelect option[value='4']").attr('selected')) {//something happens here }
}

1 个答案:

答案 0 :(得分:16)

我不知道你想要实现什么,你可以试试

$("#statsGraphingSelect").change(function() {
var n = $(this).val();
 switch(n)
 {
 case '1':
   execute code block 1
   break;
 case '2':
   execute code block 2
   break;

 }
});

现在使用您给定的标记,如果您在就绪处理程序中触发更改,如

$(function(){
 $("#statsGraphingSelect").change();
});

case 1将被执行

DEMO

相关问题