2013-06-26 102 views
0

http://jsfiddle.net/LPCpR/具有可變

<select id="menu1"> 
    <option data-icon="arrow-u" data-iconpos="top">- SELECT -</option> 
    <option data-icon="arrow-u" data-iconpos="bottom" selected="selected">Shrubs</option> 
    <option data-icon="arrow-u" data-iconpos="left">Trees</option> 
    <option data-icon="arrow-u" data-iconpos="right">Bushes</option> 
</select> 
<select id="menu2"> 
    <option data-icon="arrow-u" data-iconpos="top">- SELECT -</option> 
    <option data-icon="arrow-u" data-iconpos="bottom" selected="selected">Shrubs</option> 
    <option data-icon="arrow-u" data-iconpos="left">Trees</option> 
    <option data-icon="arrow-u" data-iconpos="right">Bushes</option> 
</select> 
$('select').change(function() { 
    if ($(this).is('#menu1')) { 
     alert('menu hone has been changed') 
    } 
}) 

上面示出了兩個菜單和一個條件替換特定條件。我的代碼中實際上有五個菜單,所以我需要簡化。

需要用分配變量的行將條件替換爲已更改菜單的ID。怎麼做?

回答

2

試試這個,

$('select').change(function() { 
    var id=$(this).attr('id'); 
    switch(id) 
    { 
     case "menu1": 
      alert('menu 1 changed'); 
      break; 
     case "menu2": 
      alert('menu 2 changed'); 
      break; 
    } 
}); 

Fiddle

+0

非常感謝。有沒有辦法將這個應用到fieldset?我意識到,每個選擇字段可能需要在一個單獨的字段集中。從這裏最簡單的方法將忽略fieldsets,但它不會發展:) 如何提醒字段集的id? http://jsfiddle.net/qYmZa/ – secr

+0

你可以在http://api.jquesry.com/closest上使用'nearest()',比如'$(this).closest('fieldset')。attr('id ');'在'onchange'函數中。 –

+0

建議本頁下面的onclick是不可取的。你能想出一種類似於你最初提出的方法,但是對於fieldset?我試過這個: http://jsfiddle.net/LPCpR/1/ – secr

0

嘗試使用id屬性來獲取ID ...嘗試以下解決方案,以檢查其菜單改變。

$('select').change(function() { 
     if ($(this)) { 
      alert('menu' + this.id + 'has been changed') 
     } 
    }) 
+0

演示版本在這裏http://jsfiddle.net/LPCpR/5/ –

1

嘗試這個

<select id="menu1" onchange="changeEvent(this);"> 
    <option data-icon="arrow-u" data-iconpos="top">- SELECT -</option> 
    <option data-icon="arrow-u" data-iconpos="bottom" selected="selected">Shrubs</option> 
    <option data-icon="arrow-u" data-iconpos="left">Trees</option> 
    <option data-icon="arrow-u" data-iconpos="right">Bushes</option> 
</select> 
<select id="menu2" onchange="changeEvent(this);"> 
    <option data-icon="arrow-u" data-iconpos="top">- SELECT -</option> 
    <option data-icon="arrow-u" data-iconpos="bottom" selected="selected">Shrubs</option> 
    <option data-icon="arrow-u" data-iconpos="left">Trees</option> 
    <option data-icon="arrow-u" data-iconpos="right">Bushes</option> 
</select> 

JS

function changeEvent(obj) 
{ 
    alert(obj.id); 
} 

Demo

+0

我wouldnt推薦使用'onchange'。它古老而笨重。並降低性能和可維護性 – krishgopinath

+0

它看起來非常整齊,謝謝。 – secr

0

試試這個

$('select').change(function() { 

     alert( $(this).attr('id') + 'has been changed') 

}) 
0
http://jsfiddle.net/LPCpR/12/ 


<select id="menu_one"> 
<option data-icon="arrow-u" data-iconpos="top">- SELECT -</option> 
<option data-icon="arrow-u" data-iconpos="bottom" selected="selected">Shrubs</option> 
<option data-icon="arrow-u" data-iconpos="left">Trees</option> 
<option data-icon="arrow-u" data-iconpos="right">Bushes</option> 
</select> 
<select id="menu_two"> 
<option data-icon="arrow-u" data-iconpos="top">- SELECT -</option> 
<option data-icon="arrow-u" data-iconpos="bottom" selected="selected">Shrubs</option> 
<option data-icon="arrow-u" data-iconpos="left">Trees</option> 
<option data-icon="arrow-u" data-iconpos="right">Bushes</option> 
</select> 

JS:

$('select').change(function() { 

     var menuNo = $(this).attr("id").split("_"); 
     alert('menu '+menuNo[1]+' has been changed'); 

    }); 

你可以看到這一解決方案的JS小提琴。