根據您的要求,您需要根據第一個下拉菜單更新第二個下拉選項。首先爲下拉菜單分配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