JQuery 选择下拉更改事件 - 选择一个默认值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9168760/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
JQuery select drop down change event - select a default
提问by user1040259
How would I still allow my change event to happen for my drop down while selecting a default value?
在选择默认值时,我如何仍然允许我的下拉列表发生更改事件?
Select value 1 as default, but also have the change function work..
选择值 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 }
}
回答by Rafay
i dont know what are you trying to achieve, you can try
我不知道你想达到什么目的,你可以试试
$("#statsGraphingSelect").change(function() {
var n = $(this).val();
switch(n)
{
case '1':
execute code block 1
break;
case '2':
execute code block 2
break;
}
});
now with your given markup if you trigger change in the ready handler like
现在使用给定的标记,如果您在就绪处理程序中触发更改,例如
$(function(){
$("#statsGraphingSelect").change();
});
the case 1
will be executed
在case 1
将被执行