当用户更改一个选项时,如何更新多个下拉菜单 [英] How to update multiple dropdown selects when user changes one selection

查看:89
本文介绍了当用户更改一个选项时,如何更新多个下拉菜单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些HTML / jQuery代码,显示三个菜单。当用户改变第一个菜单中的选择时,第二个菜单被重新加载。当用户更改第二个菜单中的选择时,第三个菜单被重新加载。我真正想要的是当菜单1选择被更改时重新加载菜单2和菜单3,并且当菜单2选择被重置时重新加载菜单3。我不知道该怎么做是让我的匿名附加功能重新加载多个菜单的选择集。另外,我想泛化代码,以便当菜单(n)被人类更新时,右边的菜单,菜单(n + 1),菜单(n + 2),...都被更新。否则,我的代码将不会扩展。谢谢。



这是我的代码。

 < html> 
< head>
< script src =http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js>< / script>
< script>
$(function(){
var selectValues = {
1:{
1-1:1-1,
1- 2:1-2
},
2:{
2-1:2-1,
2-2: 2-2
},
1-1:{
1-1-1:1-1-1,
1-1- 2:1-1-2
},
1-2:{
1-2-1:1-2-1,
1-2-2:1-2-2
},
2-1:{
2-1-1:2-1- 1,
2-1-2:2-1-2
},
2-2:{
2-2-1 :2-2-1,
2-2-2:2-2-2
}
};
var $ menu1 = $(' select.menu-1');
var $ menu2 = $('select.menu-2');
var $ menu3 = $('select.menu-3');
$ menu1.change(function(){
$ menu2.empty()。append(function(){
var output ='';
$ .each(selectValues [$ menu1.val ()],function(key,value){
output + ='< option>'+ key +'< / option>';
});
返回输出;
});
})。change();
$ menu2.change(function(){
$ menu3.empty()。append(function(){
var output ='';
$ .each(selectValues [ $ menu2.val()],function(key,value){
output + ='< option>'+ key +'< / option>';
});
返回输出;
});
})。change();
});
< / script>
< / head>
< body>

< select class =menu-1>
< option value =1> 1< / option>
< option value =2> 2< / option>
< / select>

< select class =menu-2>
< option>< / option>
< / select>
< select class =menu-3>
< option>< / option>
< / select>
< / body>
< / html>

这是一个小提琴



更新3/15。



我拿过Jake Wolpert的想法,做了几个修改,但还没有运气



这是我的新HTML:

 < div class = 菜单 > 
< select>
< option value =1> 1< / option>
< option value =2> 2< / option>
< / select>
< select>
< option>< / option>
< / select>
< select>
< option>< / option>
< / select>
< / div>

这是我的新的jQuery代码,我尝试使用nextAll重置所有菜单后,用户更改。


$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $
var val = $(this).val()
console.log(val,$ menus.index(this))

$(this).nextAll()。 empty()。html(function(){
var output ='';
$ .each(selectValues [val],function(key,value){
output + ='选项>'+ key +'< / option>';
});
返回输出;
})
})。 $ b

这是下一个小提琴的迭代。



< a href =https://jsfiddle.net/xxxfqtzj/4/ =nofollow> https://jsfiddle.net/xxxfqtzj/4/



问题是nextAll里面的代码是使用val作为selectionValues的索引,这样当我改变第一个菜单时,第二个和第三个菜单加载了相同的菜单nto他们我需要以某种方式获取我正在处理的菜单的索引,并使用该增量索引引用的selectValues中的值加载下一个菜单。

解决方案

如果你想要缩放,你不能假装知道你有多少个菜单。



https://jsfiddle.net/jakecigar/xxxfqtzj/3/ 不关心有多少菜单,只是他们是一个另一个。

  var $ menus = $(。menu select)
$ menus.change(function() {
var val = $(this).val()
console.log(val,$ menus.index(this))

$(this).next() .empty()。prop({disabled:false})。html(function(){
var output ='';
$ .each(selectValues [val],function(key,value)
输出+ ='< option>'+ key +'< / option>';
});
返回输出;
})nextAll() ({DIS abled:true})。empty()
})。first()。change()


I've got some HTML/jQuery code that displays three menus. When the user changes the selection in the first menu then the second menu gets reloaded. When the users changes the selection in the second menu then the third menu gets reloaded. What I really want is to reload menu2 and menu3 when the menu1 selection is changed and reload menu3 when the menu2 selection is reset. What I don't know how to do is have my anonymous append function reload the selection set for more than one menu. Additionally, I'd like to generalize the code so that when menu(n) is updated by a human then the menus to the right, menu(n+1), menu(n+2), ... are all updated. Otherwise, my code won't scale. Thanks.

Here is my code.

<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script>
$(function() {
   var selectValues = {
      "1": {
         "1-1": "1-1",
         "1-2": "1-2"
      },
      "2": {
         "2-1": "2-1",
         "2-2": "2-2"
       },
      "1-1": {
         "1-1-1": "1-1-1",
         "1-1-2": "1-1-2"
      },
      "1-2": {
         "1-2-1": "1-2-1",
         "1-2-2": "1-2-2"
      },
      "2-1": {
         "2-1-1": "2-1-1",
         "2-1-2": "2-1-2"
      },
      "2-2": {
         "2-2-1": "2-2-1",
         "2-2-2": "2-2-2"
      }
   };
   var $menu1 = $('select.menu-1');
   var $menu2 = $('select.menu-2');
   var $menu3 = $('select.menu-3');
   $menu1.change(function() {
      $menu2.empty().append(function() {
         var output = '';
         $.each(selectValues[$menu1.val()], function(key, value) {
             output += '<option>' + key + '</option>';
         });
         return output;
      });
   }).change();
   $menu2.change(function() {
      $menu3.empty().append(function() {
         var output = '';
         $.each(selectValues[$menu2.val()], function(key, value) {
             output += '<option>' + key + '</option>';
         });
         return output;
      });
   }).change();
});
</script>
</head>
<body>

<select class="menu-1">
   <option value="1">1</option>
   <option value="2">2</option>
</select>

<select class="menu-2">
   <option></option>
</select>
<select class="menu-3">
   <option></option>
</select>
</body>
</html>

Here is a fiddle.

Update 3/15.

I took Jake Wolpert's idea and made a couple of modifications but still no luck.

Here's my new HTML:

<div class="menu">
    <select>
        <option value="1">1</option>
        <option value="2">2</option>
    </select>
    <select>
        <option></option>
    </select>
    <select>
        <option></option>
    </select>
</div>

Here's my new jQuery code, where I try using nextAll to reset all menus after the one the user changed.

var $menus = $(".menu select")
$menus.change(function(){
   var val = $(this).val() 
   console.log(val, $menus.index(this) )

    $(this).nextAll().empty().html(function(){
        var output = '';
        $.each(selectValues[val], function (key, value) {
            output += '<option>' + key + '</option>';
        });
        return output;
    })
}).first().change()

Here is the next fiddle iteration.

https://jsfiddle.net/xxxfqtzj/4/

The problem is that the code inside nextAll is using val as the index to selectValues so that when I change the first menu the second and third ones get the same menus loaded into them. I need to somehow get the index of the menu that I'm processing and load the next menu with values in selectValues that are referenced by that incrementing index.

解决方案

If you want it to scale, you can’t pretend to know how many menus you have.

https://jsfiddle.net/jakecigar/xxxfqtzj/3/ Does not care about how many menus, just that they are one after another.

var $menus = $(".menu select")
$menus.change(function(){
   var val = $(this).val() 
   console.log(val, $menus.index(this) )

    $(this).next().empty().prop({disabled:false}).html(function(){
        var output = '';
        $.each(selectValues[val], function (key, value) {
            output += '<option>' + key + '</option>';
        });
        return output;
    }).nextAll().prop({disabled:true}).empty()
}).first().change()

这篇关于当用户更改一个选项时,如何更新多个下拉菜单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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