Ich möchte, dass der Benutzer einen "Bundesland/Bundesland" aus dem ersten Dropdown auswählt, dann die angegebenen Gebiete in diesem Bundesland/Bundesland, aber ich möchte ein "anderes" in das zweite hinzufügen Dropdown für Bereiche, die nicht aufgelistet sind, aber wenn der Benutzer "andere" auswählt, möchte ich, dass das Eingabefeld angezeigt wird.Den Wert des Arrays für ein anderes Array verwenden
Ich bin ziemlich neu in js und ich habe ein kleines Problem, das ich nicht beheben kann. Entschuldigung, wenn es gefragt wird, aber ich habe versucht, ein ähnliches Problem ohne Erfolg zu finden. Auch ich konnte das nicht an jsfiddle arbeiten, also entschuldige ich mich für den langen Beitrag.
Die meisten der Optionen deaktiviert (immer noch ein work in progress)
Ich habe ein funktionierendes Beispiel von dem, was ich hier tun wollen:
Betrachten Sie bitte den Code-Schnipsel.
function filterDropDownLists(provinces, cityfilter) {
var city1= new Array('Please Select Area','Example 1', 'Example 2', 'Example 3');
/*values for second dropdown*/
/*if one of these optios are selected, display specified array above*/
switch (provinces.value) {
case 'Gauteng':
cityfilter.options.length = 0;
for (i = 0; i < city1.length; i++) {
createOption(cityfilter, city1[i], city1[i]);
}
break;
default:
cityfilter.options.length = 1;
}
}
function createOption(provincefilter, text, value) { /*add the values to the next dropdown */
var opt = document.createElement('option');
opt.value = value;
opt.text = text;
provincefilter.options.add(opt);
}
\t
\t
<select class="provincefilter" id="provincefilter" onchange="filterDropDownLists(this,document.getElementById('cityfilter'))">
\t \t \t \t <option value="selectprovince" selected>Please Select Province</option>
\t \t \t \t <option value="Gauteng">Gauteng</option>
\t \t \t </select>
\t \t \t <select class="cityfilter" id="cityfilter"> <!--second dropdown to list specified array-->
\t \t \t \t <option value="selectarea" selected>Please Select Area</option>
\t \t \t
\t \t \t </select>
Aber die, die ich mit einem Problem habe verwendet eine Anordnung, um die Werte einzufügen. Was ich will, ist für die Person, um etwas in der ersten, dann zeigt es bestimmte Werte für die Sekunde, aber für die Sekunde gibt es eine Option von 'anderen', ich möchte ein Eingabefeld daneben, falls ' ‚andere wurde ausgewählt
Problem:
function filterDropDownLists_rblock(provinces, cityfilter) { \t \t
\t \t var gauteng = new Array('Please Select Area','Pretoria East', 'Pretoria North', 'Pretoria Central', 'Johannesburg', 'Midrand', 'Centurion','Other');
var easterncape = new Array('Please Select Area','Port Elizabeth', 'Durban','Other');
\t \t var freestate = new Array('Please Select Area','Louis Trichardt','Other');
\t \t
\t \t
switch (provinces.value) {
case 'Gauteng':
cityfilter.options.length = 0;
for (i = 0; i < gauteng.length; i++) {
createOption(cityfilter, gauteng[i], gauteng[i]);
}
break;
default:
cityfilter.options.length = 1;
}
}
function createOption(provincefilter, text, value) {
var opt = document.createElement('option');
opt.value = value;
opt.text = text;
provincefilter.options.add(opt);
}
function city_other_select(otherCitySelect)
{
if(otherCitySelect){
otherCityOptionValue = document.getElementById("city_option_other").value;
if(otherCityOptionValue == otherCitySelect.value){
document.getElementById("rblock_area_other").style.display = "inline-block";
}
else{
document.getElementById("rblock_area_other").style.display = "none";
}
}
else{
document.getElementById("rblock_area_other").style.display = "none";
}
}
<div class="rblock_business_area">
\t \t \t <select class="rblock_provincefilter" id="rblock_provincefilter" onchange="filterDropDownLists_rblock(this,document.getElementById('rblock_cityfilter'))">
\t \t \t \t <option value="selectprovince" selected>Please Select Province</option>
\t \t \t \t <option value="Gauteng">Gauteng</option>
\t \t \t </select>
\t \t \t <select class="rblock_cityfilter" id="rblock_cityfilter" onchange="city_other_select(this)">
\t \t \t \t <option value="rblock_selectarea" selected>Please Select Area</option>
\t \t \t \t <option value="city_option_other" id="city_option_other">Other</option>
\t \t \t </select>
\t \t \t <div class="rblock_area_other" style="display:none;">
\t \t \t \t <input type=text name=areaother size="30px" placeholder="Please specify"/>
\t \t \t </div>
\t \t </div>
Sie haben die Frage zu verkürzen. Und auch du erste Code-Box, der ganze Code kann wie 10 Zeilen nicht mehr sein. – Alon
Sorry, ich habe vergessen, diesen Teil zu bearbeiten, es ist ein Platzhalter für andere Bereiche, die ich hinzufügen möchte –
Sie müssen den Code verständlicher bitte machen. –