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

I have two boboxs.i want to set second bobox values on the basis of first bobox values on change.like i

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

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745649579a4638179.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信