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 的行。怎么做?

4

5 回答 5

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

小提琴

于 2013-06-26T04:26:51.447 回答
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);
 }

演示

于 2013-06-26T04:29:41.267 回答
0

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

  $('select').change(function () {
        if ($(this)) {
            alert( 'menu' + this.id + 'has been changed')
        }
    })
于 2013-06-26T04:29:29.243 回答
0

尝试这个

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

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

})
于 2013-06-26T04:32:09.083 回答
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 fiddle。

于 2013-06-26T04:38:39.273 回答