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

use*_*259 4 jquery if-statement function

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

选择值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 }
}
Run Code Online (Sandbox Code Playgroud)

Raf*_*fay 16

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

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

 }
});
Run Code Online (Sandbox Code Playgroud)

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

$(function(){
 $("#statsGraphingSelect").change();
});
Run Code Online (Sandbox Code Playgroud)

case 1将被执行

DEMO