2017-03-23 117 views
0

我有一个应用程序使用yii2框架。 我正在尝试使用select选项(下拉选择),并使用Kartik Select2PHP:设置第二个下拉选项基于第一个下拉选择选项使用kartik Select2

这是我的选择选项 enter image description here

的观点在你上面的图片可以看到我已经两个选择的选择,但我的条件。这是条件。

我的第一个下拉选项(事务ID)包含2个选项,有

  1. 购买

,如果用户选择1. Buy,第二个下拉选项(付款方式)将显示

  1. 现金
  2. 作为选项的电子货币交易 ,并且如果用户选择2. Sell,将仅显示1. Cash作为选项。

如何使用Kartik Select2做到这一点?

任何帮助将被折衷。

感谢。

+0

会需要您已有的相关代码。 – bassxzero

回答

0

根据您的要求,您需要根据第一个下拉菜单更新第二个下拉选项。首先为下拉菜单分配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); 
相关问题