Here is my jsfiddle: http://jsfiddle.net/rynslmns/W9TLT/推荐答案您没有在小提琴中包含jquery.You didn't include jquery in your fiddle.也就是说,您实际上不应该使用全局变量,而应该创建自己的对象并稍加修改代码:That said, you shouldn't really use a global, instead make your own object and modify your code slightly:$(function(){ //setup arrays var cars = { "Chevrolet" : ['Silverado','Suburban','Tahoe'], "Ford" : ['F150','Taurus','Pinto','Bronco'], "Toyota" : ['Camry','Tacoma','4Runner'], "GMC" : ['blah1','blah2','blah3'] }; $('#make').change(function() { $("#models").html(""); //clear existing options var newOptions = cars[this.value]; //finds the array w/the name of the selected value //populate the new options for (var i=0; i<newOptions.length; i++) { $("#models").append("<option>"+newOptions[i]+"</option>"); } $('#models').prop('disabled', false); //enable the dropdown });}); http://jsfiddle.net/W9TLT/10/另一个说明.在代码的开头禁用下拉菜单,然后在代码末尾启用下拉菜单,除了启用下拉菜单外没有其他作用.由于浏览器/javascript的单线程性质,禁用从未真正发生过.重要的是代码完成后将其设置为什么.Another note. Disabling the dropdown at the start of your code and then enabling it at the end has no effect other than to enable the dropdown. Due to the single-threaded nature of browsers/javascript, the disable never really happens. All that matters is what it is set to when the code completes. 这篇关于javascript使用选择框填充其他选择框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
07-29 12:42
查看更多