检测特定< option>被jQuery选中 [英] Detect when a specific <option> is selected with jQuery

查看:191
本文介绍了检测特定< option>被jQuery选中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下下拉菜单:

 < select name ='type'id ='type'> ; 
< option id ='trade_buy'value ='1'selected ='selected'>购买< / option>
< option id ='trade_buy_max'value ='1'>购买max< / option>
< option id ='trade_sell'value ='2'>卖< / option>
< option id ='trade_sell_max'value ='2'> Sell max< / option>
< / select>

我希望jQuery检测到id trade_buy_max 被选中。



我尝试过以下操作,但似乎不起作用。


$ b $($ {code code)$(document).ready(function(){

$(option#trade_buy_max
$ b //做某事

});
});

任何想法?



提前感谢。

解决方案

这个工程...
听选择框上的更改事件,然后只需拉取所选项的id属性。

  $(#type)。change(function() b $ b var id = $(this).find(option:selected)。attr(id); 

switch(id){
casetrade_buy_max:
//在这里做某事
break;
}
});


I have the following drop-down menu:

<select name='type' id='type'>
    <option id='trade_buy' value='1' selected='selected'>Buy</option>
    <option id='trade_buy_max' value='1'>Buy max</option>
    <option id='trade_sell' value='2'>Sell</option>
    <option id='trade_sell_max' value='2'>Sell max</option>
</select>

I'd like jQuery to detect when the option with the id trade_buy_max is selected.

I've tried the following, but it doesn't seem to work.

$(document).ready(function() {

    $("option#trade_buy_max").select(function () {

        //do something

    });
});

Any ideas?

Thanks in advance.

解决方案

This works... Listen for the change event on the select box to fire and once it does then just pull the id attribute of the selected option.

$("#type").change(function(){
  var id = $(this).find("option:selected").attr("id");

  switch (id){
    case "trade_buy_max":
      // do something here
      break;
  }
});

这篇关于检测特定&lt; option&gt;被jQuery选中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆