[英]Populate drop down list from another drop down list in php and javascript
[英]JQuery/Javascript - Drop Down List get info from another Drop Down
我有2個下拉菜單。 在第一個中,選擇一個模型,在第二個中,選擇一個特定模型。 http://jsfiddle.net/QskM9/
例如:
drop down 1:
Nokia
Samsung
Apple -if this is selected, the second drop down shows:
Drop down 2:
iPhone 3
iPhone 3Gs
iPhone 4 -When this is selected, go to www.apple.com (for example)
我整天都在嘗試,似乎無法完成這項工作。 到目前為止,這是我得到的:
// HTML
<form method="get" name="box">
<select id="opsaetning" name="opsaetning" onchange="changelist(this)">
<option selected="selected">Model</option>
<option label="Apple">Apple</option>
<option label="Nokia">Nokia</option>
<option label="Samsung">Samsung</option>
</select>
<select id="model" name="model" onchange="document.location=$(this).val();">
<option value="www.apple.com" selected="selected">iphone 4</option>
</select>
</form>
//SCRIPT
<script type="text/javascript" language="javascript">
var lists = new Array();
// Apple
lists['Apple'] = new Array();
lists['Apple'][0] = new Array(
'Forbered Gmail til mobilen');
lists['Apple'] = new Array(
'www.apple.com/ihphone3g',
'www.apple.com/iphone4');
function changelist(){
list = lists[box.options[box.selectedIndex].value];
sletListe(box.form.model);
fyldListe(box.form.model, list);
}
function sletListe(box){
while(box.options.length)box.options[0] = null;
}
function fyldListe(box, lists){
for(i=0; i< arr[0].length; i++){
option = new Option(arr[0][i], arr[l][i]);
box.options[box.length] = option;
}
box.selectedIndex=0;
}
</script>
誰能幫我使它正常工作? 注意我不能使用PHP,.NET,Perl等。只能使用HTML和js / jq。
這是您要找的東西嗎? http://jsfiddle.net/sEB5k/4/ (在下面)或http://jsfiddle.net/npvym/14/ (在下面)
var lists = new Array();
lists['Model'] = new Array();
lists['Apple'] = new Array(
'www.apple.com/ihphone3g',
'www.apple.com/iphone4');
lists['Nokia'] = new Array(
'http://www.nokiausa.com/us-en/products/phone/e7-00/',
'http://www.nokiausa.com/us-en/products/phone/c6-01/');
lists['Samsung'] = new Array(
'http://www.samsung.com/us/mobile/cell-phones/SPH-D710ZKASPR',
'http://www.samsung.com/us/mobile/cell-phones/SGH-T989ZKBTMB');
function changelist(select){
list = lists[select.options[select.selectedIndex].value];
sletListe(select.form.model);
fyldListe(select.form.model, list);
}
function sletListe(box){
while(box.options.length)box.options[0] = null;
}
function fyldListe(box, list){
for(i=0; i< list.length; i++){
option = new Option(list[i], list[i]);
box.options[i] = option;
}
box.selectedIndex=0;
}
要么
我也接受了Przemek的答案,並對其進行了大量修改,使其符合我的想法。 我認為它比我的原始解決方案更漂亮,並且具有更好的設計。
HTML
<select id="company" name="company">
<option value="nokia" selected="selected">Nokia</option>
<option value="samsung">Samsung</option>
<option value="apple">Apple</option>
</select>
<select id="nokia" name="product" class="product">
<option value="#" selected="selected">Choose product</option>
<option value="http://www.nokiausa.com/us-en/products/phone/e7-00/">E7-00</option>
<option value="http://www.nokiausa.com/us-en/products/phone/c6-01/">C6-01</option>
</select>
<select id="samsung" style="display:none" class="product">
<option value="#" selected="selected">Choose product</option>
<option value="http://www.samsung.com/us/mobile/cell-phones/SGH-T989ZKBTMB">SGH-T989ZKBTMB</option>
<option value="http://www.samsung.com/us/mobile/cell-phones/SPH-D710ZKASPR">SPH-D710ZKASPR</option>
</select>
<select id="apple" style="display:none" class="product">
<option value="#" selected="selected">Choose product</option>
<option value="http://www.apple.com/iphone/iphone-4/specs.html">iPhone 4</option>
<option value="http://www.apple.com/iphone/iphone-3gs/specs.html">iPhone 3GS</option>
</select>
JavaScript的
$("#company").change(function() {
$('select[name="product"]').removeAttr("name").hide();
$("#" + $(this).val()).show().attr("name", "product");
});
$(".product").change(function() {
document.location = $(this).val();
});
試試看:
var options = {
"Apple" : {
'ihphone3g': 'http://www.apple.com/ihphone3g',
'iphone4': 'http://www.apple.com/iphone4'
}
}
function changelist(v){
var $t = $("#model");
//clear old options
$t.html('');
//fill up new options
if(options[v]){
for(var i in options[v]){
if(options[v].hasOwnProperty(i)){
$t.append('<option value="' + options[v][i] + '">' + i + '<\/option>')
}
}
}
}
我想到了這樣的東西:
HTML:
<select id="company" name="company">
<option value="nokia" selected="selected">Nokia</option>
<option value="samsung">Samsung</option>
<option value="apple">Apple</option>
</select>
<select id="nokia" name="product">
<option value="1" selected="selected">6300</option>
<option value="2">3210</option>
<option value="3">3310</option>
</select>
<select id="samsung" style="display:none">
<option value="1" selected="selected">Galaxy S</option>
<option value="2">Galaxy S2</option>
<option value="3">Galaxy Tab</option>
</select>
<select id="apple" style="display:none">
<option value="1" selected="selected">iPhone 3</option>
<option value="2">iPhone 4</option>
<option value="3">iPhone 4S</option>
</select>
jQuery的:
$("#company").change(function() {
$('select[name="product"]').removeAttr("name").hide();
$("#" + $(this).val()).show().attr("name", "product");
});
function getCo(cid){
var countryVal=$('#cmbCountry').val();
$.post("<?php echo url_for('country/ajaxloadcourse') ?>", //Ajax file
{ cid: cid }, // create an object will all values
//function that is called when server returns a value.
function(data) {
var selectbox="<select name='province' id='province' class='formSelect' style='width: 150px;' tabindex='4' onchange='getCourseId(this.value,"+countryVal+")'>";
selectbox = selectbox +"<option value=''><?php echo __('--Select--') ?></option>";
$.each(data, function(key, value) {
selectbox=selectbox +"<option value="+key+">"+value+"</option>";
});
selectbox=selectbox +"</select>";
$('#provincelist').html(selectbox);
},
//How you want the data formated when it is returned from the server.
"json"
);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.