我的网站有3项服务,我希望能够点击服务1并查看菜单1,点击服务2并查看菜单2,点击服务3并查看菜单3.我有以下代码但它是没有按预期工作。当我点击服务2和服务3时,什么都没有显示出来。
HTML:
<div class="row" id="menu_service1">
<div class="col-md-4">
<span><h5>Choose a line</h5> <select id="lines" class="form-control"></select></span>
<span><h5>Choose a stop</h5> <select id="stop" class="form-control"></select></span>
</div>
</div>
<div class="row" id="menu_service2">
<div class="col-md-4">
<span><h5>Choose a stop</h5> <select id="stop" class="form-control"></select></span>
<span><h5>Choose a stop</h5> <select id="stop" class="form-control"></select></span>
</div>
</div>
<div class="row" id="menu_service3">
<div class="col-md-4">
<p>blah</p>
</div>
</div>
JS:
$(document).ready(function() {
var line_array = ["Line 1", "Line 2", "Line 3"];
var stops = ["stop1", "stop2", "stop3"] ;
for (var i = 0; i < line_array.length; i++) {
$('#lines').append('<option>' + line_array[i] + '</option>');
}
for (var i = 0; i < stops.length; i++) {
$('#stop').append('<option>' + stops[i] + '</option>');
}
$('#menu_service1').hide();
$('#menu_service2').hide();
$('#menu_service3').hide();
$('#Service_1').click(function() {
$('#menu_service1').toggle();
});
$('#Service_2').click(function() {
$('#menu_service2').toggle();
});
$('#Service_3').click(function() {
$('#menu_service3').toggle();
});
});
答案 0 :(得分:3)
<强> Working Fiddle 强>
id
在同一文档中应该是唯一的,因此请尝试用通用类替换重复的文档,例如:
<span><h5>Choose a line</h5> <select class="form-control lines"></select></span>
<span><h5>Choose a stop</h5> <select class="form-control stop"></select></span>
在你的JS中使用它们与类选择器点.
,如:
$('.lines').append('<option>' + line_array[i] + '</option>');
您还应该通过添加ID选择器sogn #
来修复以下两行中的拼写错误:
$('menu_service2').toggle();
$('menu_service2').toggle();
应该是:
$('#menu_service2').toggle();
$('#menu_service2').toggle();
此外,您可以为多个选择器使用逗号,
分隔符,而不是:
$('#menu_service1').hide();
$('#menu_service2').hide();
$('#menu_service3').hide();
仅使用:
$('#menu_service1,#menu_service2,#menu_service3').hide();
希望这有帮助。
$(document).ready(function() {
var line_array = ["Line 1", "Line 2", "Line 3"];
var stops = ["stop1", "stop2", "stop3"] ;
for (var i = 0; i < line_array.length; i++) {
$('.lines').append('<option>' + line_array[i] + '</option>');
}
for (var i = 0; i < stops.length; i++) {
$('.stop').append('<option>' + stops[i] + '</option>');
}
$('#menu_service1,#menu_service2,#menu_service3').hide();
$('#Service_1').click(function() {
$('.row').hide();
$('#menu_service1').toggle();
});
$('#Service_2').click(function() {
$('.row').hide();
$('#menu_service2').toggle();
});
$('#Service_3').click(function() {
$('.row').hide();
$('#menu_service3').toggle();
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link href="https://netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://netdna.bootstrapcdn.com/bootstrap/3.0.0/js/bootstrap.min.js"></script>
<button id='Service_1'>Service 1</button>
<button id='Service_2'>Service 2</button>
<button id='Service_3'>Service 3</button>
<div class="row" id="menu_service1">
<div class="col-md-4">
<span><h5>Choose a line1</h5> <select class="form-control lines"></select></span>
<span><h5>Choose a stop1</h5> <select class="form-control stop"></select></span>
</div>
</div>
<div class="row" id="menu_service2">
<div class="col-md-4">
<span><h5>Choose a line2</h5> <select class="form-control lines"></select></span>
<span><h5>Choose a stop2</h5> <select class="form-control stop"></select></span>
</div>
</div>
<div class="row" id="menu_service3">
<div class="col-md-4">
<p>blah</p>
</div>
</div>
答案 1 :(得分:1)
您忘记了选择器中ID的前面的#符号。
$('#menu_service2').toggle();
$('#menu_service3').toggle();
答案 2 :(得分:0)
我更喜欢编写更少的代码行来获得相同的结果。
$(function () {
var line_array = ["Line 1", "Line 2", "Line 3"];
var stops = ["stop1", "stop2", "stop3"] ;
for (var i = 0; i < line_array.length; i++) {
$('[id^=lines]').append('<option>' + line_array[i] + '</option>');
}
for (var i = 0; i < stops.length; i++) {
$('[id^=stop]').append('<option>' + stops[i] + '</option>');
}
$('[id^=menu_service]').hide();
$('[id^=Service_]').click(function() {
var currDiv = this.getAttribute('data-div');
$('div.row:not(#' + currDiv + ')').hide();
$('#' + currDiv).toggle();
});
});
&#13;
<script src="http://code.jquery.com/jquery-1.11.3.min.js"></script>
<button id='Service_1' data-div="menu_service1">Service 1</button>
<button id='Service_2' data-div="menu_service2">Service 2</button>
<button id='Service_3' data-div="menu_service3">Service 3</button>
<div class="row" id="menu_service1">
<div class="col-md-4">
<span><h5>Choose a line</h5> <select id="lines1" class="form-control"></select></span>
<span><h5>Choose a stop</h5> <select id="stop1" class="form-control"></select></span>
</div>
</div>
<div class="row" id="menu_service2">
<div class="col-md-4">
<span><h5>Choose a stop</h5> <select id="stop2" class="form-control"></select></span>
<span><h5>Choose a stop</h5> <select id="stop2" class="form-control"></select></span>
</div>
</div>
<div class="row" id="menu_service3">
<div class="col-md-4">
<p>blah</p>
</div>
</div>
&#13;