使用jquery下拉框

时间:2011-06-30 08:57:34

标签: javascript jquery

我有两个下拉框。一个下拉框的值可能另一个为1.现在我将第一个下拉值更改为6月但不会更改第二个下拉框值。这里没有传递第二个下拉框,因为我已经开始使用点击事件。这里我使用直播(点击),但我这样做没有点击第二个下拉框,该值也应该通过

 **Updated**

 $(firstselect).live(click,function)
 $(secondselect).live(click,function)

1 个答案:

答案 0 :(得分:1)

现在我明白了这个问题:

  

第二个选择框应显示先前在第一个选择的值。

你可以这样做:

var prevValue = $('#firstselect').val();

$('#firstselect').change(function() {
   $('#secondselect').val(prevValue);
   prevValue = this.value;
});

DEMO

双向:

var prevValue = {
    'firstselect':  $('#firstselect').val(),
    'secondselect':  $('#secondselect').val()
};

$('select').change(function() {
    var other = this.id === 'firstselect' ? 'secondselect' : 'firstselect';

    prevValue[other] = $('#' + other).val();
    $('#' + other).val(prevValue[this.id]);
    prevValue[this.id] = this.value;
});

DEMO 2