关于jquery:使引导下拉按钮级联

Make bootstrap drop down buttons cascading

我在我的网页上添加了两个下拉框,如下所示,但我想使它们层叠,然后将下拉按钮的值传递给一个函数。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
   <button data-toggle="dropdown" class="btn btn-success dropdown-toggle">Success <span class="caret"></span></button>
      <ul class="dropdown-menu">
         
<li>
Action
</li>

         
<li>
Another action
</li>

         
<li>
Something else here
</li>

         <li class="divider">
</li>

         
<li>
Separated link
</li>

     
</ul>

<!-- /btn-group -->

   <button data-toggle="dropdown" class="btn btn-info dropdown-toggle">Info <span class="caret"></span></button>
      <ul class="dropdown-menu">
         
<li>
Action
</li>

         
<li>
Another action
</li>

         
<li>
Something else here
</li>

         <li class="divider">
</li>

         
<li>
Separated link
</li>

     
</ul>

<!-- /btn-group -->

  • 你说的级联是什么意思?我还建议您构建一个引导程序并添加代码(包括JS函数)。
  • stackoverflow.com/a/9773450/3917754请查看@andris ilich的答案我想这是您想要的
  • 谢谢你的链接…但是我想要的是假设用户从第一个下拉列表中选择一个国家。然后,应在第二个下拉列表中填充与所选国家/地区相关的所有州
  • 您需要一个与所选国家相关的数组。之后就相当容易了。
  • @按不一定,您也可以使用枚举来完成任务。只需将Country枚举映射到State枚举。
  • 你能给我一些代码吗?我对这个很陌生……
  • @Kylet我从来没有在javascript中使用过枚举。
  • @按一下,看看这个问题stackoverflow.com/questions/287903/enums-in-javascript。它们与您在答案中使用数组的方式非常相似。


正如Kylet在注释中提到的,您可以用枚举代替数组来完成。不过,作为一个舒适的选择,我将使用一个数组。小提琴

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
var states = [
   "Kansas",
   "Oklahoma",
   "Texas"    
];

$('.dropdown-menu > li > a').click(function() {
    yourFunction(this.text);
});

function yourFunction(val) {
       
    if ( val ==="Action" ) {
        $('#states').html('');
        for ( var i in states ) {
            $('#states').append("
<li>
" + states[i] +"
</li>
");
        }
    }
   
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js">
<script src="//maxcdn.bootstrapcdn.com/bootstrap/3.3.2/js/bootstrap.min.js">
<link href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap.min.css" rel="stylesheet"/>

  <button data-toggle="dropdown" class="btn btn-success dropdown-toggle">Success <span class="caret"></span>
  </button>
  <ul class="dropdown-menu">
   
<li>
Action
   
</li>

   
<li>
Another action
   
</li>

   
<li>
Something else here
   
</li>

    <li class="divider">
</li>

   
<li>
Separated link
   
</li>

 
</ul>


<!-- /btn-group -->

  <button data-toggle="dropdown" class="btn btn-info dropdown-toggle">Info <span class="caret"></span>
  </button>
  <ul class="dropdown-menu" id="states">
   
<li>
Action
   
</li>

   
<li>
Another action
   
</li>

   
<li>
Something else here
   
</li>

    <li class="divider">
</li>

   
<li>
Separated link
   
</li>

 
</ul>


<!-- /btn-group -->

  • 令人惊叹的!!!!我在这方面工作了将近3个小时。太神了!谢谢!