根据您的要求,您需要根据第一个下拉菜单更新第二个下拉选项。首先为下拉菜单分配ID,第一个ID将是第一个下拉菜单,第二个ID将是第二个下拉菜单。在您的第一个下拉更改事件中添加以下代码。
// get first dropdown value here
var firstDropdownValue = $("#first-dropdown").select2("val");
var $secondDropdown = $('#second-dropdown');
// get all second dropdown options
var options = $secondDropdown.data('select2').options.options;
// delete all options of the native select element
$secondDropdown.html('');
// build new items
var items = [];
items.push({
"id": 1, // value of option
"text": 'Cash' // name of option
});
$secondDropdown.append("<option value=\"1\">Cash</option>");
// check the first dropdown value and add E-money option
if(firstDropdownValue == 1){
items.push({
"id": 2,
"text": 'E-money'
});
$secondDropdown.append("<option value=\"2\">E-money</option>");
}
// add new items
options.data = items;
$secondDropdown.select2(options);
会需要您已有的相关代码。 – bassxzero