检测何时使用jQuery选择特定的
我有以下下拉菜单:
Buy Buy max Sell Sell max
我想让jQuery检测何时选择了id为trade_buy_max
的选项。
我尝试了以下,但似乎没有用。
$(document).ready(function() { $("option#trade_buy_max").select(function () { //do something }); });
有任何想法吗?
提前致谢。
这样可以…在选择框上监听更改事件以便触发,然后只需拉出所选选项的id属性。
$("#type").change(function(){ var id = $(this).find("option:selected").attr("id"); switch (id){ case "trade_buy_max": // do something here break; } });
你需要做的是为select
添加一个onchange
处理程序:
$('#type').change(function(){ if($(this).val() == 2){ /* Do Something */ } });
您可以在其选择上绑定change
事件,然后检查是否选择了选项
$("select#type").change(function () { if( $("option#trade_buy_max:selected").length ) { // do something here } });
$("option#trade_buy_max").change(function () { opt = $(this).children("option:selected").attr('id'); if(opt == '#trade_sell_max'){ // do stuff } });
未经测试,但这应该工作。
使用change
事件并获取所选选项的id
属性:
$('#type').change(function () { var selectedId = $('option:selected', this).attr('id'); if (selectedId == "trade_buy_max") { // do something } });
更改.select to .change并在#之前放置空间