仅第二次进行onselect工作 [英] onselect work from the second time only

查看:47
本文介绍了仅第二次进行onselect工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有下面的代码按需工作,但是on select仅在第二次选择更改中起作用.

I have below code working as desired but the on select works only from the second select change.

有什么解决办法吗?

工作示例

<html>
<head>

<title>select & select</title>


<script type='text/javascript'>

var select2data = {

  'English': [['One', 1], ['Two', 2], ['Three', 3]],
  'Spanish': [['Uno', 1], ['Dos', 2], ['Tres', 3]]
};


function change_second_select(){
var sel1 = document.getElementById('select1')
  , sel2 = document.getElementById('select2');
sel1.onchange = function() {
  var os = select2data[sel1.value]; // Get the options required by select1.
  if (os) {
    sel2.options.length = 0; // Clear the options for select2.
    for (var i=0; i<os.length; i++) {
      var o = new Option(os[i][0], os[i][1]);
      try { // Add each option, allowing for browser differences.
        sel2.add(o);
      } catch (ex) {
        sel2.add(o, null);
      }
    }
    sel2.selectedIndex = 0;

  }
  return true;
};
}


</script>


<select id="select1" onchange="change_second_select();">
<option value="English">English</option>
<option value="Spanish">Spanish</option>
</select>
<select id="select2">

</select>


</head>
    </html>

推荐答案

将您的change_on_select更改为以下内容:

Change your change_on_select to as below:

工作示例@ http://jsfiddle.net/3dCyw/1/

function change_second_select(){
var sel1 = document.getElementById('select1')
  , sel2 = document.getElementById('select2');
  var os = select2data[sel1.value]; // Get the options required by select1.
  if (os) {
    sel2.options.length = 0; // Clear the options for select2.
    for (var i=0; i<os.length; i++) {
      var o = new Option(os[i][0], os[i][1]);
      try { // Add each option, allowing for browser differences.
        sel2.add(o);
      } catch (ex) {
        sel2.add(o, null);
      }
    }
    sel2.selectedIndex = 0;

  }
  return true;
}

这篇关于仅第二次进行onselect工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆