如何根据值过滤值并将其替换为相应的名称

时间:2013-11-18 12:36:26

标签: javascript php jquery yii

我是Jquery的新手 我有两个由Yii Widget创建的下拉列表。以下是两个下拉列表的代码:

<select id="SearchForm_min_cost_select" style="display: none;">
<option value="0"></option>
<option value="10 lakhs">10 lakhs</option>
<option value="20 lakhs">20 lakhs</option>
<option value="30 lakhs">30 lakhs</option>
<option value="40 lakhs">40 lakhs</option>
<option value="50 lakhs">50 lakhs</option>
<option value="60 lakhs">60 lakhs</option>
<option value="70 lakhs">70 lakhs</option>
<option value="80 lakhs">80 lakhs</option>
<option value="90 lakhs">90 lakhs</option>
<option value="1 crores">1 crores</option>
<option value="2 crores">2 crores</option>
<option value="3 crores">3 crores</option>
<option value="4 crores">4 crores</option>
<option value="5 crores">5 crores</option>
</select>

<select id="SearchForm_max_cost_select" style="display: none;">
<option value="0"></option>
<option value="10 lakhs">10 lakhs</option>
<option value="20 lakhs">20 lakhs</option>
<option value="30 lakhs">30 lakhs</option>
<option value="40 lakhs">40 lakhs</option>
<option value="50 lakhs">50 lakhs</option>
<option value="60 lakhs">60 lakhs</option>
<option value="70 lakhs">70 lakhs</option>
<option value="80 lakhs">80 lakhs</option>
<option value="90 lakhs">90 lakhs</option>
<option value="1 crores">1 crores</option>
<option value="2 crores">2 crores</option>
<option value="3 crores">3 crores</option>
<option value="4 crores">4 crores</option>
<option value="5 crores">5 crores</option>
</select>  

OnSelect ,我调用以下函数cost_change(item.value)

function cost_change(price) {


    var match =  <?php echo json_encode(Yii::app()->params['match_resales']);?>;
    console.log("match",match);    
    var value=match[price];
    console.log("value",value);

      jQuery('#SearchForm_max_cost_select').html(jQuery('#SearchForm_min_cost_select').html())     
    var toKeep = jQuery('#SearchForm_max_cost_select option').filter( function( ) {
        return parseInt(this.value) > parseInt( value);       
 } );        
   console.log("to keep",toKeep);

   jQuery('#SearchForm_max_cost_select').html(toKeep);

} 

'match_resales' => array('10 lakhs'=>'1000000.0','20 lakhs'=>'2000000.0','30 lakhs'=>'3000000.0','40 lakhs'=>'4000000.0','50 lakhs'=>'5000000.0','60 lakhs'=>'6000000.0','70 lakhs'=>'7000000.0','80 lakhs'=>'8000000.0','90 lakhs'=>'9000000.0','1 crores'=>'10000000.0','2 crores'=>'20000000.0','3 crores'=>'30000000.0','4 crores'=>'40000000.0','5 crores'=>'50000000.0'),     

现在,在上面的代码中,基于selected value我将键值对数组编码为键值对的JSON数组,找到与变量价格对应的值并将其存储在变量中value
在上面的代码中,我需要操作函数,使它找到大于value的所有选项,并在第二个下拉列表中显示它。

修改 我从JSON数组中检索value之前一直都是正确的。之后,我想找到大于value的所有值,并在第二个下拉列表中显示相应的值

1 个答案:

答案 0 :(得分:0)

假设两个列表相同:

// Keep a copy of the default options
var $options = $('#SearchForm_min_cost_select').children().clone();

// Within your change handler:

var index = $('#SearchForm_min_cost_select').find(':selected').index() + 1;
$('#SearchForm_max_cost_select').html($options).children(':lt('+index+')').remove();

免责声明:这很可能会针对性能进行优化,但理解起来相当简单。