我支持一些遗留代码,并在实现新功能时遇到一些问题。
我有2个多选项,第一个显示状态列表,第二个显示城市列表。我们的想法是,城市列表将根据第一个选择中的选定状态进行过滤。
目前,城市包含在一个javascript数组中,该数组在if语句中用于将相关选项附加到select,但是这仅适用于单个状态选择。
如何在多个州选择中过滤城市?
我尝试了很多if语句组合而没有运气。我的jquery知识显然缺乏,并且没有在google / stackoverflow中找到类似的东西 - 但是我很欣赏我可能会从错误的角度接近这个。
<form name="newform">
<select multiple size="10" name="getStateSelect" id="getStateSelectID" onchange="GetSeries(document.newform.getStateSelect.options [document.newform.getStateSelect.selectedIndex].value);">
<option value="1">Alaska</option>
<option value="2">Arizona</option>
<option value="3">Californa</option>
</select>
<br />
<br />
<select id="getCitySelect" multiple="multiple" size="10"></select>
</form>
var fsArray = [{id: 1,stateid: 1,cityname: "Anchorage"},
{id: 2,stateid: 1,cityname: "Fairbanks"},
{id: 3,stateid: 1,cityname: "Wasilla"},
{id: 4,stateid: 2,cityname: "Flagstaff"},
{id: 5,stateid: 2,cityname: "Phoenix"},
{id: 6,stateid: 2,cityname: "Tucson"},
{id: 7,stateid: 3,cityname: "Fremont"},
{id: 8,stateid: 3,cityname: "Lakeport"},
{id: 9,stateid: 3,cityname: "Los Angeles"}];
function GetSeries(i) {
var SeriesSelectBox = document.getElementById("getCitySelect");
SeriesSelectBox.options.length = 0;
for (j in fsArray) {
if (fsArray[j].stateid == i) {
var seriesLength = SeriesSelectBox.options.length;
SeriesSelectBox.options[seriesLength] = new Option(fsArray[j].cityname, fsArray[j].id);
}
}
}
http://jsfiddle.net/fcp3h7mw/1/
任何帮助非常感谢! 感谢
答案 0 :(得分:1)
我刚刚在getStateSelectID值上添加了一个for循环来检查每个元素是否有选择状态,如果选中它会运行你的循环来添加城市。
小提琴:http://jsfiddle.net/fcp3h7mw/5/
的javascript:
var fsArray = [
{id:1,stateid:1,cityname:"Anchorage"},
{id:2,stateid:1,cityname:"Fairbanks"},
{id:3,stateid:1,cityname:"Wasilla"},
{id:4,stateid:2,cityname:"Flagstaff"},
{id:5,stateid:2,cityname:"Phoenix"},
{id:6,stateid:2,cityname:"Tucson"},
{id:7,stateid:3,cityname:"Fremont"},
{id:8,stateid:3,cityname:"Lakeport"},
{id:9,stateid:3,cityname:"Los Angeles"}
];
function GetSeries(i) {
var id = document.getElementById("getStateSelectID");
var SeriesSelectBox = document.getElementById("getCitySelect");
SeriesSelectBox.options.length = 0;
var states = 0;
for (states=0; states < id.length; states++) {
if(id[states].selected){
for (j in fsArray) {
if (fsArray[j].stateid == id[states].value) {
var seriesLength = SeriesSelectBox.options.length;
SeriesSelectBox.options[seriesLength] = new Option(fsArray[j].cityname, fsArray[j].id);
}
}
}
}
}
标记:我还在标记中取出了很多函数调用
<select multiple size="10" name="getStateSelect" id="getStateSelectID" onchange="GetSeries();">
答案 1 :(得分:0)
我已更新您的代码,以便能够选择多个状态并显示正确的城市。
基本上,你需要获得一个带有属性&#34;选择&#34;的选项列表。并将选项值存储在数组中(onchange)
之后,您将遍历新数组并匹配数组中的值而不是单个值。
以下是更新后的代码:http://jsfiddle.net/biz79/fcp3h7mw/3/
var fsArray = [
{id:1,stateid:1,cityname:"Anchorage"},
{id:2,stateid:1,cityname:"Fairbanks"},
{id:3,stateid:1,cityname:"Wasilla"},
{id:4,stateid:2,cityname:"Flagstaff"},
{id:5,stateid:2,cityname:"Phoenix"},
{id:6,stateid:2,cityname:"Tucson"},
{id:7,stateid:3,cityname:"Fremont"},
{id:8,stateid:3,cityname:"Lakeport"},
{id:9,stateid:3,cityname:"Los Angeles"}
];
function getSeries() {
var SeriesSelectBox = document.getElementById("getCitySelect");
SeriesSelectBox.options.length = 0;
var arr = getStateSelected();
for (var i = 0; i< arr.length; i++) {
for (var j in fsArray) {
if (fsArray[j].stateid == arr[i]) {
var seriesLength = SeriesSelectBox.options.length;
SeriesSelectBox.options[seriesLength] = new Option(fsArray[j].cityname, fsArray[j].id);
}
}
}
}
function getStateSelected() {
var sel = document.querySelectorAll('#getStateSelectID option');
var arr = [];
for (var i = 0; i< sel.length; i++ ) {
if ( sel[i].selected ) {
arr.push(sel[i].value);
}
}
return arr;
}
希望这会有所帮助。如果您有疑问,请告诉我。