我正在尝试弹出一个依赖于另一个选择框的选择框
<select>
<option value="a">a</option>
<option value="b">b</option>
</select>
<select>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
</select>
我希望例如当用户选择select1(a)时,Select2显示1,2和3 当他选择B时,它显示4,5,6等等。
selectbox值是一个PHP ARRAY对象,我已经尝试使用$.each
来填充它,但是由于数组是这样的:
{ "Option 1":[ {"name" : "test", "age" : "1"}, .... ] }
提前致谢。
答案 0 :(得分:1)
例如,你有json:
{
"Apple": {
"iphone SE": "https://www.apple.com/iphone-se/",
"iphone 7": "https://www.apple.com/iphone-7/"
},
"samsung": {
"galaxy s8": "http://www.samsung.com/us/explore/galaxy-s8/",
"galaxy s7": "http://www.samsung.com/us/explore/galaxy-s7/"
}
}
为此,我们必须使用javascript并使用jQuery(javascript库)。看看下面的示例:
$(function() {
var selectValues = {
"apple": {
"iphone SE": "https://www.apple.com/iphone-se/",
"iphone 7": "https://www.apple.com/iphone-7/"
},
"samsung": {
"galaxy s8": "http://www.samsung.com/us/explore/galaxy-s8/",
"galaxy s7": "http://www.samsung.com/us/explore/galaxy-s7/"
}
};
var $vendor = $('select.mobile-vendor');
var $model = $('select.model');
$vendor.change(function() {
$model.empty().append(function() {
var output = '';
$.each(selectValues[$vendor.val()], function(key, value) {
output += '<option>' + key + '</option>';
});
return output;
});
}).change();
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<p>
<select class="mobile-vendor">
<option value="apple">Apple</option>
<option value="samsung">Samsung</option>
</select>
<select class="model">
<option></option>
</select>
</p>
&#13;
答案 1 :(得分:0)
以下是如何使用jquery实现更改的示例。
请稍微发布/解释您的对象格式,我将创建一个循环来正确解析它。
<!DOCTYPE html>
<html lang="en">
<head>
<title>Bootstrap Example</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</head>
<body>
<select id="letter">
<option value="a">a</option>
<option value="b">b</option>
</select>
<select id="age">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
</select>
<script>
$("#letter").change(function(){
if($("#letter").val() === "a"){
$("#age")
.empty()
.append('<option selected="selected" value="1">1</option>')
;
}else{
$("#age")
.empty()
.append('<option selected="selected" value="5">5</option>')
;
}
});
</script>
</body>
</html>
答案 2 :(得分:0)
// you can try something like this.
var s1 = document.getElementById("s1");
var s2 = document.getElementById("s2");
var s1Value = s1.value;
var i;
var frag = document.createDocumentFragment();
var ele;
if(s1Value == 'a'){
for(i=0;i<3;i++){
ele = document.createElement('option');
ele.value = i + 1;
ele.innerHTML = i + 1;
frag.appendChild(ele);
}
}
s2.innerHTML = ''
s2.appendChild(frag);
答案 3 :(得分:0)
谢谢大家的帮助,但是我采用了另一种方法。这是在select as选项中输出整个PHP数组,并将它们全部隐藏起来,当选择select1中的一个选项时,我使用jQuery只显示具有特定属性的项:
<script>
$(".select2").prop("disabled", true);
$(".select1").change(function() {
$('.select2').prop('disabled', false);
$(".select2 option").hide();
var currentVal = $('.select1 :selected').attr('optionis'),
ValueSelected = $(".select2 option[optionvalue*='" + currentVal + "']");
ValueSelected.show();
$(ValueSelected).first().prop("selected", true);
});
</script>
<select class="select1">
<option value="1" optionis="1">Option 1</option>
<option value="2" optionis="2">Option 2</option>
</select>
<select class="select2">
<option value="1" optionvalue="1">Value 1</option>
<option value="2" optionvalue="1">Value 2</option>
<option value="1" optionvalue="2">Value 3</option>
<option value="1" optionvalue="2">Value 4</option>
<option value="1" optionvalue="2">Value 5</option>
</select>
<强> CODEPEN 强>