最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - Populate combobox values with another combo values in js - Stack Overflow

matteradmin7PV0评论

I have two boboxs.i want to set second bobox values on the basis of first bobox values on change.like if i three values (1,2,3) for first one and on select 1 i populate second one with values (11,12,13) and same for others.I can do it in php but don't know how to do it in js or jquery.Thanks

I have two boboxs.i want to set second bobox values on the basis of first bobox values on change.like if i three values (1,2,3) for first one and on select 1 i populate second one with values (11,12,13) and same for others.I can do it in php but don't know how to do it in js or jquery.Thanks

Share Improve this question asked Mar 17, 2014 at 18:26 AddaAdda 3055 silver badges18 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 4

Here is how you can do this:

JavaScript

var first = document.getElementById('select-input'),
    second = document.getElementById('second-select-input');

first.onchange = function (e) {
  var val = e.target.value;
  empty(second);
  for (var i = 0; i < 3; i += 1) {
    addOption(val + '' + (i + 1), second);
  }
};

function empty(select) {
  select.innerHTML = '';
}

function addOption(val, select) {
  var option = document.createElement('option');
  option.value = val;
  option.innerHTML = val;
  select.appendChild(option);
}

HTML

<select id="select-input">
  <option value="1">1</option>
  <option value="2">2</option>
  <option value="3">3</option>
</select>
<select id="second-select-input">
</select>

DEMO

Post a comment

comment list (0)

  1. No comments so far