如何过滤选项..。
当我选择城市电影和影院后,我选择电影或影院来改变电影播放或影院播放电影.
如果我选择了一个选项来显示与选项相关的信息
下面是我的html代码
$(document).ready(function() {
var cityData = [{
cityName: 'Bengaluru',
value: "Bengaluru",
data: [{
movieName: 'ABC',
theaterName: 'Tulsi Theatre'
},
{
movieName: 'DEF',
theaterName: 'PVR'
},
{
movieName: 'GHI',
theaterName: 'Srinivasa Theatre'
}
]
},
{
cityName: 'Hyderabad',
value: "Hyderabad",
data: [{
movieName: '123',
theaterName: 'Theatre1'
},
{
movieName: '456',
theaterName: 'PVR2'
},
{
movieName: '789',
theaterName: 'Theatre3'
}
]
},
{
cityName: 'Guntur',
value: "Guntur",
data: [{
movieName: 'ABC1',
theaterName: 'Theatre4'
},
{
movieName: 'DEF2',
theaterName: 'PVR3'
},
{
movieName: 'GHI3',
theaterName: 'Theatre5'
}
]
},
{
cityName: 'Ongole',
value: "Ongole",
data: 'currently not available'
}
];
$("#selectCity").on('change', function() {
var locations = cityData.filter(c => c.cityName === $(this).val())[0].data;
var locationString = '';
var locationString2 = '';
console.log(locations)
$.each(locations, function(i, item) {
console.log(JSON.stringify(item));
locationString += '<option value="' + item.theaterName + '">' + item.theaterName + '</option>';
locationString2 += '<option value="' + item.movieName + '">' + item.movieName + '</option>';
});
$('#secondselectbox').html(locationString);
$('#thirdselectbox').html(locationString2);
});
});<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="UserData">
<h1>MyMovie-Ticket-Booking</h1>
<select class="selectCity" id="selectCity">
<option value="City">Select City</option>
<option value="Bengaluru">Bengaluru</option>
<option value="Hyderabad">Hyderabad</option>
<option value="Guntur">Guntur</option>
<option value="Ongole">Ongole</option>
</select>
<span id="welcome"> </span>
</div>
<div>
<select id="secondselectbox"></select>
<select id="thirdselectbox"></select>
</div>
如何更改在第一个下拉列表中选择电影名称以更改另一个下拉列表中的相关影院名称.
更改电影名称,并显示电影放映的地方……
发布于 2017-12-14 12:40:03
$(document).ready(function() {
var cityData = [{
cityName: 'Bengaluru',
value: "Bengaluru",
data: [{
movieName: 'ABC',
theaterName: 'Tulsi Theatre'
},
{
movieName: 'DEF',
theaterName: 'PVR'
},
{
movieName: 'GHI',
theaterName: 'Srinivasa Theatre'
}
]
},
{
cityName: 'Hyderabad',
value: "Hyderabad",
data: [{
movieName: '123',
theaterName: 'Theatre1'
},
{
movieName: '456',
theaterName: 'PVR2'
},
{
movieName: '789',
theaterName: 'Theatre3'
}
]
},
{
cityName: 'Guntur',
value: "Guntur",
data: [{
movieName: 'ABC1',
theaterName: 'Theatre4'
},
{
movieName: 'ABC1',
theaterName: 'PVR3'
},
{
movieName: 'GHI3',
theaterName: 'Theatre5'
}
]
},
{
cityName: 'Ongole',
value: "Ongole",
data: 'currently not available'
}
];
$("#selectCity").on('change', function() {
var locations = cityData.filter(c => c.cityName === $(this).val())[0].data;
var locationString2 = '';
console.log(locations)
$.each(locations, function(i, item) {
locationString2 += '<option value="' + item.movieName + '">' + item.movieName + '</option>';
});
$('#thirdselectbox').html(locationString2);
createTheaterDropdown();
});
$("#thirdselectbox").on('change', function() {
createTheaterDropdown();
});
function createTheaterDropdown(){
var locations = cityData.filter(c => c.cityName === $('#selectCity').val())[0].data;
var movie = locations.filter(c => c.movieName === $('#thirdselectbox').val());
var locationString = '';
$.each(movie, function(i, item) {
locationString += '<option value="' + item.theaterName + '">' + item.theaterName + '</option>';
})
$('#secondselectbox').html(locationString);
}
})<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<h1>MyMovie-Ticket-Booking</h1>
<select class="selectCity" id="selectCity">
<option value="City">Select City</option>
<option value="Bengaluru">Bengaluru</option>
<option value="Hyderabad">Hyderabad</option>
<option value="Guntur">Guntur</option>
<option value="Ongole">Ongole</option>
</select>
<span id="welcome"> </span>
</div>
<div>
<select id="thirdselectbox"></select>
<select id="secondselectbox"></select>
</div>
您可以在选择“城市名称”之后过滤剧院名称,这里是该名称的代码。
$("#selectCity").on('change', function () {
var locations = cityData.filter(c => c.cityName === $(this).val())[0].data;
var locationString2 = '';
console.log(locations)
$.each(locations, function (i, item) {
locationString2 += '<option value="' + item.movieName + '">' + item.movieName + '</option>';
});
$('#thirdselectbox').html(locationString2);
createTheaterDropdown();
});
$("#thirdselectbox").on('change', function () {
createTheaterDropdown();
});
function createTheaterDropdown() {
var locations = cityData.filter(c => c.cityName === $('#selectCity').val())[0].data;
var movie = locations.filter(c => c.movieName === $('#thirdselectbox').val());
var locationString = '';
$.each(movie, function (i, item) {
locationString += '<option value="' + item.theaterName + '">' + item.theaterName + '</option>';
})
$('#secondselectbox').html(locationString);
}发布于 2017-12-14 13:06:33
或者,您可以通过添加一个自定义属性(即:data-theater=item.theaterName )来确保每部电影都与影院相关联。
然后,您可以使用影院下拉列表的更改事件来隐藏/显示相关的电影,而不是不断地重新构建DOM。
$(document).ready(function() {
var cityData = [{
cityName: 'Bengaluru',
value: "Bengaluru",
data: [{
movieName: 'ABC',
theaterName: 'Tulsi Theatre'
},
{
movieName: 'DEF',
theaterName: 'PVR'
},
{
movieName: 'GHI',
theaterName: 'Srinivasa Theatre'
}
]
},
{
cityName: 'Hyderabad',
value: "Hyderabad",
data: [{
movieName: '123',
theaterName: 'Theatre1'
},
{
movieName: '456',
theaterName: 'PVR2'
},
{
movieName: '789',
theaterName: 'Theatre3'
}
]
},
{
cityName: 'Guntur',
value: "Guntur",
data: [{
movieName: 'ABC1',
theaterName: 'Theatre4'
},
{
movieName: 'DEF2',
theaterName: 'PVR3'
},
{
movieName: 'GHI3',
theaterName: 'Theatre5'
}
]
},
{
cityName: 'Ongole',
value: "Ongole",
data: 'currently not available'
}
];
$("#selectCity").on('change', function() {
var locations = cityData.filter(c => c.cityName === $(this).val())[0].data;
var locationString = '';
var locationString2 = '';
console.log(locations)
$.each(locations, function(i, item) {
console.log(JSON.stringify(item));
locationString += '<option value="' + item.theaterName + '">' + item.theaterName + '</option>';
locationString2 += '<option data-theater="' + item.theaterName + '" value="' + item.movieName + '">' + item.movieName + '</option>';
});
$('#secondselectbox').append(locationString);
$('#thirdselectbox').append(locationString2);
// ensure the movies are initialized correctly based on the current theater
theaterChanged.call($('#secondselectbox'));
});
$("#secondselectbox").on('change', theaterChanged);
function theaterChanged() {
var theater = this.value;
$('#thirdselectbox option').attr('hidden', true);
$('#thirdselectbox option[data-theater="' + theater + '"]').attr('hidden', false);
$("#thirdselectbox option[hidden!=hidden]").eq(0).prop("selected", true);
}
});<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="UserData">
<h1>MyMovie-Ticket-Booking</h1>
<select class="selectCity" id="selectCity">
<option value="City">Select City</option>
<option value="Bengaluru">Bengaluru</option>
<option value="Hyderabad">Hyderabad</option>
<option value="Guntur">Guntur</option>
<option value="Ongole">Ongole</option>
</select>
<span id="welcome"> </span>
</div>
<div>
<select id="secondselectbox"></select>
<select id="thirdselectbox"></select>
</div>
https://stackoverflow.com/questions/47813237
复制相似问题