开发者

Set selected value of second select based on specific values chosen from first select

I've got a JQuery problem that I can't seem to get my head around - I have a SELECT that, depending on the user's choice, needs to set the selected value of a related select to a particular value.

For example:

SELECT A OPTIONS
box
wheel
rabbit

In ps开发者_StackOverflow中文版eudocode, it would go something like "IF SELECT A SELECTED VALUE ONCHANGE is "rabbit", SET SELECT B SELECTED VALUE to "hutch" OR IF SELECT A SELECTED VALUE ONCHANGE is "box", SET SELECT B SELECTED VALUE to "container"

I know once I see the solution I'll kick myself, but I've been braindead about this for a few days now... any help is greatly appreciated.


Something like this might work

var map = {
  "rabbit": "hutch", 
  "box": "container"
};

$("#a").change(function() {
  var newVal = map[$(this).val()];
  if (newVal) $("#b").val(newVal);
});


// create a map of values to map between:
var mapping = {
  'rabbit' : 'hutch',
  'box' : 'container'
};

// on change set the other select value accordingly.
$('#Select1').change(function() {
  $('#Select2').val(mapping[$(this).val()]);
});


Am I missing something or would this do it?

if ($('#selectA').val() == 'rabbit') {
  $('#selectB').val('hutch');
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜