用变量替换特定条件

时间:2013-06-26 04:24:34

标签: jquery conditional simplify

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')
    }
})

以上说明了两个菜单和一个条件:如果选择了菜单1,则提醒。我的代码中实际上有五个菜单,所以我需要简化。

需要用一行来替换条件,该行将变量分配给已更改菜单的id。怎么做?

5 个答案:

答案 0 :(得分: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

答案 1 :(得分: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

答案 2 :(得分:0)

尝试使用id属性获取id ...尝试使用以下解决方案来检查更改了哪个菜单。

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

答案 3 :(得分:0)

试试这个

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

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

})

答案 4 :(得分: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小提琴。