【问题标题】:How to reset the first dropdownlist when greater than second dropdownlist当大于第二个下拉列表时如何重置第一个下拉列表
【发布时间】:2013-11-17 23:53:29
【问题描述】:

我有两个下拉列表。下面是两个下拉列表的代码。

<select id="SearchForm_min_cost_select" style="display: none;">
    <option value="0"></option>
    <option value="1lakh">1lakh</option>
    <option value="2lakh">2lakh</option>
    <option value="3lakh">3lakh</option>
    <option value="4lakh">4lakh</option>
    <option value="5lakh">5lakh</option>
    <option value="6lakh">6lakh</option>
</select>
</br>
<select id="SearchForm_max_cost_select" style="display: none;">
    <option value="0"></option>
    <option value="1lakh">1lakh</option>
    <option value="2lakh">2lakh</option>
    <option value="3lakh">3lakh</option>
    <option value="4lakh">4lakh</option>
    <option value="5lakh">5lakh</option>
    <option value="6lakh">6lakh</option>
</select>

我使用下面的 jQuery 代码创建了一个依赖下拉列表并且工作正常。这个函数在分钟内被调用

<script>
function cost_change(price) {
    var removed;

    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())     
    jQuery('#SearchForm_max_cost_select').prepend(removed);

    var toKeep = jQuery('#SearchForm_max_cost_select option').filter( function( ) {
        return parseInt(this.value) > parseInt( price);       
    });        

    console.log("to keep",toKeep);

    removed =  jQuery('#SearchForm_max_cost_select option').filter( function( ) {
        return parseInt(this.value) < parseInt( price);
    });

    jQuery('#SearchForm_max_cost_select').html(toKeep);
}
</script>

现在,当我在第二个下拉列表中选择的最大值小于第一个下拉列表中的最小值时,第一个下拉列表的值应重置为第二个。假设我在 min 中选择 30 万,在 max 中选择 20 万,那么 min 的值应该自动设置为 20 万。如何做到这一点?

【问题讨论】:

    标签: jquery


    【解决方案1】:

    你实际上需要更多的逻辑来激发你想要的行为。

    演示: http://jsfiddle.net/573Yf/

    var i=$('#min'),x=$('#max'),a,b;
    $(i).change(function(){
        a=i[0]; b=x[0];
        if(a.selectedIndex>b.selectedIndex) {
            b.selectedIndex = a.selectedIndex;
        }
    });
    $(x).change(function(){
        a=i[0]; b=x[0];
        if(b.selectedIndex<a.selectedIndex) {
            a.selectedIndex = b.selectedIndex;
        }
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-08-19
      • 1970-01-01
      • 1970-01-01
      • 2018-01-17
      • 2021-11-09
      • 1970-01-01
      • 1970-01-01
      • 2013-03-21
      相关资源
      最近更新 更多