【发布时间】:2021-01-18 01:00:02
【问题描述】:
我的英语不好。对不起。
而且我不知道 Json 和 Ajax。
我想向Select2 Cascade 添加两个以上的项目。
我又创建了一个选择/选项(#model),但它不起作用。
我在 json 文件夹中创建了三个文件:animal.json、Vehicles.json、horse.json
animal.json 和 Vehicles.json 文件运行执行,而 horse.json 文件不运行执行。
当我选择马时,我希望第三个选择/选项(#model)支撑并打开。
我看到了以下页面,但我不明白。你能在这里修改或创建我的代码吗? http://ajaxray.com/blog/select2-dependent-cascading-select-list-reload/
我的Json文件如下:
{
"201" : "Horse",
"202" : "Elephant",
"203" : "Donkey",
"204" : "Camel",
"204" : "Tiger"
}
var Select2Cascade = ( function(window, $) {
function Select2Cascade(parent, child, url, select2Options) {
var afterActions = [];
var options = select2Options || {};
// Register functions to be called after cascading data loading done
this.then = function(callback) {
afterActions.push(callback);
return this;
};
parent.select2(select2Options).on("change", function (e) {
child.prop("disabled", true);
var _this = this;
$.getJSON(url.replace(':parentId:', $(this).val()), function(items) {
var newOptions = '<option value="">-- Select --</option>';
for(var id in items) {
newOptions += '<option value="'+ id +'">'+ items[id] +'</option>';
}
child.select2('destroy').html(newOptions).prop("disabled", false)
.select2(options);
afterActions.forEach(function (callback) {
callback(parent, child, items);
});
});
});
}
return Select2Cascade;
})( window, $);
$(document).ready(function() {
var select2Options = { width: 'resolve' };
var apiUrl = 'json/:parentId:.json';
$('select').select2(select2Options);
var cascadLoading = new Select2Cascade($('#type'), $('#subtype'), apiUrl, select2Options);
cascadLoading.then( function(parent, child, items) {
// Open the child listbox immediately
child.select2('open');
// Dump response data
console.log(items);
});
var cascadLoading = new Select2Cascade($('#subtype'), $('#model'), apiUrl, select2Options);
cascadLoading.then( function(parent, child, items) {
// Open the child listbox immediately
child.select2('open');
// Dump response data
console.log(items);
});
$('#subtype').prop('disabled', true);
$('#model').prop('disabled', true);
});
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Select2</title>
<meta http-equiv="Cache-Control" content="no-cache, no-store, must-revalidate">
<meta http-equiv="Pragma" content="no-cache">
<link href="https://cdnjs.cloudflare.com/ajax/libs/select2-bootstrap-css/1.4.6/select2-bootstrap.min.css" rel="stylesheet"/>
<link href="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.3/css/select2.min.css" rel="stylesheet"/>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.3/js/select2.min.js"></script>
</head>
<body>
<form class="form-horizontal">
<div class="form-group">
<label for="type" class="col-sm-5 control-label">I'd like to ride</label>
<div class="col-sm-5">
<select name="type" id="type" class="form-control">
<option>--Select your ride--</option>
<option value="animals">Animal</option>
<option value="vehicles">Vehicle</option>
</select>
</div>
</div>
<div class="form-group">
<label for="subtype" class="col-sm-5 control-label">More specifically</label>
<div class="col-sm-5">
<select name="subtype" id="subtype" class="form-control">
<option>-- Select type first--</option>
</select>
</div>
</div>
<div class="form-group">
<label for="model" class="col-sm-5 control-label">More specifically</label>
<div class="col-sm-5">
<select name="model" id="model" class="form-control">
<option>-- Select type first--</option>
</select>
</div>
</div>
</form>
</body></html>
【问题讨论】:
-
您想在第三个选择选项中添加什么?什么关系?
-
@aviboy2006 马子集。例如,种族的类型
-
添加了答案,但是如果您提供有关子集或示例 json 关系的更多详细信息,它将很容易解决。
-
@aviboy2006 示例,Select1:(国家)= 选项:美国、加拿大、英国、|选择 2:(美国的州)= 选项:纽约,加利福尼亚 |选择 3:(加利福尼亚市)= 选项:洛杉矶、圣地亚哥
-
您可以尝试向此值创建添加依赖关系。您的代码中唯一的问题是使用您正在做的 id。用户类保持 id 唯一。
标签: jquery json ajax select jquery-select2