Javascript 在第二次选择中显示数字时,有没有办法显示这些数字(值)的键?@RobMorris:我已经更新了答案。只需在选项中直接使用所需的文本,而不是对象的值。首先,感谢大家的帮助。我现在已经把它工作到了令人满意的程度。我不介意您在OP中查看codepen示例,如果您

Javascript 在第二次选择中显示数字时,有没有办法显示这些数字(值)的键?@RobMorris:我已经更新了答案。只需在选项中直接使用所需的文本,而不是对象的值。首先,感谢大家的帮助。我现在已经把它工作到了令人满意的程度。我不介意您在OP中查看codepen示例,如果您,javascript,jquery,Javascript,Jquery,在第二次选择中显示数字时,有没有办法显示这些数字(值)的键?@RobMorris:我已经更新了答案。只需在选项中直接使用所需的文本,而不是对象的值。首先,感谢大家的帮助。我现在已经把它工作到了令人满意的程度。我不介意您在OP中查看codepen示例,如果您有时间的话,可以整理一些东西,我对jQuery相当陌生,所以还不知道最佳实践。另外,一些淡出淡入的功能似乎不能正常工作。 <form action="" method="get" class="insurance-numbers">


在第二次选择中显示数字时,有没有办法显示这些数字(值)的键?@RobMorris:我已经更新了答案。只需在
选项中直接使用所需的文本,而不是对象的值。首先,感谢大家的帮助。我现在已经把它工作到了令人满意的程度。我不介意您在OP中查看codepen示例,如果您有时间的话,可以整理一些东西,我对jQuery相当陌生,所以还不知道最佳实践。另外,一些淡出淡入的功能似乎不能正常工作。
<form action="" method="get" class="insurance-numbers">
  <div>
    <select id="company">
      <option value="default-company">Choose a company</option>
    </select>
  </div>
  <div>
    <select id="insurance-type">
      <option value="default-type">Choose an insurance type</option>
    </select>
  </div>
  <div id="insurance-number"></div>
</form>
#insurance-type {
  display:none;
}
        var insurancecompanies = [
      { 
        name : 'Advent', 
        property : '01242 674 674', 
        fleet : '', 
        motortrade : ''
      },
      {   name : 'Allianz', 
       property : '0844 412 9988', 
       fleet : '0800 587 5858', 
       motortrade : '' 
      },
      {   name : 'Aviva', 
       property : '0800 015 1498', 
       fleet : '0800 246 876', 
       motortrade : '0800 246 876'
      },
      {   name : 'AXA', 
       property : '0870 900 0867', 
       fleet : '0870 900 0860', 
       motortrade : '0870 900 1753'
      },
      {   name : 'Catlin', 
       property : '', 
       fleet : '0800 066 5364', 
       motortrade : ''
      },
      {   name : 'Chartis', 
       property : '', 
       fleet : '0844 477 6544', 
       motortrade : ''
      },
      {   name : 'Clegg Gifford', 
       property : '', 
       fleet : '', 
       motortrade : '01708 729 529'
      },
      {   name : 'Equity Red Star', 
       property : '', 
       fleet : '0845 609 1235', 
       motortrade : ''
      },
      {   name : 'Highway/LV', 
       property : '', 
       fleet : '0845 373 1244', 
       motortrade : ''
      },
      {   name : 'NIG', 
       property : '', 
       fleet : '0845 300 4644', 
       motortrade : '0845 300 4644'
      },
      {   name : 'QBE', 
       property : '', 
       fleet : '01245 272 700', 
       motortrade : ''
      },
      {   name : 'Royal Sun Alliance', 
       property : '0845 077 0120', 
       fleet : '0845 675 0404', 
       motortrade : '0845 077 0119' 
      },
      {   name : 'Summit', 
       property : '', 
       fleet : '01254 396 655', 
       motortrade : ''
      },
      {   name : 'Zurich', 
       property : '0845 300 2055', 
       fleet : '0800 300 2055', 
       motortrade : ''
      }
    ];


    function findCompany(name) {
      var i = 0, len = insurancecompanies.length;
      for (i; i < len; i += 1) {
        if (insurancecompanies[i].name === name) {
          return insurancecompanies[i];
        }
      }
      return null;
    };

    function addInsuranceType(type) {
      if (type !== '') {
        $("#insurance-type").append('<option val="' + type + '">' + type + '</option>');
      }
    }

    var dropdown = [], i = 0, len = insurancecompanies.length;
    for (i; i < len; i += 1) {
      dropdown.push(insurancecompanies[i].name);
    }

    $.each( dropdown, function( i, val ) {
      $("#company").append( "<option val=\""+ val +"\">" + val + "</option>" );
    });

    $('#company').on('change', function() {
    var optionSelected = $("option:selected", this);
    var valueSelected = this.value;
    if(valueSelected !== 'default-company') {
      $('#insurance-type').children().remove();
      $('#insurance-number').text('');
      var selectedInsurance = findCompany(valueSelected);
      addInsuranceType(selectedInsurance.property, 'Property');
      addInsuranceType(selectedInsurance.fleet, 'Fleet');
      addInsuranceType(selectedInsurance.motortrade, 'motor trade');
      $('#insurance-type').fadeIn();
    } else {
      $('#insurance-type').fadeOut();
      $('#insurance-number').fadeOut();
    }
});

$('#insurance-type').on('change', function() {
   var optionSelected = $("option:selected", this);
   var insuranceSelected = this.value;
  $('#insurance-number').text(insuranceSelected);
  $('#insurance-number').fadeIn();
});
function findCompany(name) {
  var i = 0, len = insurancecompanies.length;
  for (i; i < len; i += 1) {
    if (insurancecompanies[i].name === name) {
      return insurancecompanies[i];
    }
  }
  return null;
};
var selectedInsurance = findCompany(valueSelected);
for (i in selectedInsurance) {
  $("#insurance-type").append( "<option val=\""+ selectedInsurance[i] +"\">" + selectedInsurance[i] + "</option>" );
}
var selectedInsurance = findCompany(valueSelected);
if (selectedInsurance.property !== '') {
  $("#insurance-type").append('<option val="' + selectedInsurance.property + '">' + selectedInsurance.property + '</option>');
}
if (selectedInsurance.fleet !== '') {
  $("#insurance-type").append('<option val="' + selectedInsurance.fleet + '">' + selectedInsurance.fleet + '</option>');
}
if (selectedInsurance.motortrade !== '') {
  $("#insurance-type").append('<option val="' + selectedInsurance.motortrade + '">' + selectedInsurance.motortrade + '</option>');
}
function addInsuranceType(type) {
  if (type !== '') {
    $("#insurance-type").append('<option val="' + type + '">' + type + '</option>');
  }
}
var selectedInsurance = findCompany(valueSelected);
addInsuranceType(selectedInsurance.property);
addInsuranceType(selectedInsurance.fleet);
addInsuranceType(selectedInsurance.motortrade);
if (selectedInsurance.property !== '') {
  $("#insurance-type").append('<option val="' + selectedInsurance.property + '">property</option>');
}
// ... and so on for the other two
function addInsuranceType(value, text) {
  if (value !== '') {
    $("#insurance-type").append('<option val="' + value + '">' + text + '</option>');
  }
}
var selectedInsurance = findCompany(valueSelected);
addInsuranceType(selectedInsurance.property, 'property');
// ... and so on for the other two
function addInsuranceType(object, type) {
  if (object[type] !== '') {
    $("#insurance-type").append('<option val="' + object[type] + '">' + type + '</option>');
  }
}
var selectedInsurance = findCompany(valueSelected);
addInsuranceType(selectedInsurance, 'property');
// ... and so on for the other two