无法从阵列中获取多个链接的选择框

时间:2014-08-17 05:20:07

标签: jquery arrays select

尝试在加载时填充第一个框,第二个框更改为第一个框,第三个框更改为第二个框。在我添加第三个和代码以尝试使其工作之前我已经使用了代码,但是由于尝试获得第三个和第二个链接,我没有运气让第一个甚至填充。任何帮助将不胜感激。

http://jsfiddle.net/4a9f8go5/

HTML code:

 <select name="leather1" id="leather1"><option disabled="true" selected>-Select Diameter-</option></select>
<select name="leather2" id="leather2"><option disabled="true" selected>-Select Color-</option></select>
<select name="leather3" id="leather3"><option disabled="true" selected>-Select Clasp-</option></select>

JQuery代码:

    var leather12 = [
    {
     "value" : "0",
     "diameter" : "4mm",
     "colors" : ['black','color2','color3'],
     "black" : ['locking','magnetic'],
     "color2" : ['locking2','magnetic2'],
     "color3" : ['locking3','magnetic3']
    },
    {
     "value"   : "1",
     "diameter"   : "4mmd",
     "colors"  : ['black1','color22','color32'],
     "black1"  : ['locking1','magnetic1'],
     "color22"  : ['locking21','magnetic21'],
     "color32"  : ['locking31','magnetic31']
    }
   ] ;

$(function() {
 size2color = [] ;
 for(var i=0; i<leather12.length; i++) {
  size2color[leather12[i].diameter] = leather12[i].colors ;
 }
});

$(function() { 
  color2clasp = [] ;
  for(var i=2; i<leather12[getobjectbyid("leather1").value].length; i++) {
   color2clasp[leather12[getobjectbyid("leather1").value].colors] = leather12[getobjectbyname("leather1").value][this.value][i] ;
  }
});

$(function() {
 // populate diameter select box
 var options = '' ;
 for (var i = 0; i < leather12.length; i++) {
  options += '<option value="' + leather12[i].value + '">' + leather12[i].diameter + '</option>'; 
 }
 $("#leather1").html(options);   // populate select box with array

 // selecting leather1 (change) will populate leather2 select box
 $("#leather1").bind("change",
   function() {
    $("#leather2").children().remove() ;        // clear select box
    var options = '<option disabled="true" selected>-Select Color-</option>' ;
    for (var i = 0; i < size2color[this.value].length; i++) { 
     options += '<option value="' + size2color[this.value][i] + '">' + size2color[this.value][i] + '</option>'; 
    }
    $("#leather2").html(options);   // populate select box with array
   }
 );

 //selecting leather2 (change) will populate leather 2 select box
 $("#leather2").bind("change",
   function() {
    $("#leather3").children().remove();
    var options = '<option disabled="true" selected>-Select Clasp-</option>' ;
    for (var i = 0; i < color2clasp[this.value].length; i++) { 
     options += '<option value="' + color2clasp[this.value][i] + '">' + color2clasp[this.value][i] + '</option>'; 
    }
    $("#leather3").html(options);
   }
 );
  });

1 个答案:

答案 0 :(得分:0)

使用以下小提琴:

你的color2clasp在getobjectbyid(“leather1”)投掷错误。值。 此外,当一个jquery行发生错误时,下面的代码行不会被执行。所以select不绑定。

http://jsfiddle.net/60m5np85/

    var leather12 = [
    {
     "value"   : "0",
     "diameter"  : "4mm",
     "colors"  : ['black','color2','color3'],
     "black"  : ['locking','magnetic'],
     "color2"  : ['locking2','magnetic2'],
     "color3"  : ['locking3','magnetic3']
    },
    {
     "value"   : "1",
     "diameter"   : "4mmd",
     "colors"  : ['black1','color22','color32'],
     "black1"  : ['locking1','magnetic1'],
     "color22"  : ['locking21','magnetic21'],
     "color32"  : ['locking31','magnetic31']
    }
   ] ;


$(function() {    

 // populate diameter select box
 var options = '<option disabled="true" selected>-Select Diameter-</option>' ;
 for (var i = 0; i < leather12.length; i++) {
  options += '<option value="' + leather12[i].value + '">' + leather12[i].diameter + '</option>';
 }
 $("#leather1").html(options);   // populate select box with array

 // selecting leather1 (change) will populate leather2 select box
 $("#leather1").bind("change",
   function() {
    $("#leather2").children().remove() ;        // clear select box
    var options = '<option disabled="true" selected>-Select Color-</option>' ;
       var colors = leather12[this.value]["colors"];
    for (var i = 0; i < colors.length; i++) { 
     options += '<option value="' + colors[i] + '">' + colors[i] + '</option>'; 
    }
    $("#leather2").html(options);   // populate select box with array       
   }                                                              
 );

 //selecting leather2 (change) will populate leather 2 select box
 $("#leather2").bind("change",
   function() {

    $("#leather3").children().remove();
    var options = '<option disabled="true" selected>-Select Clasp-</option>' ;
       var clasp = leather12[$("#leather1").val()][$("#leather2").val()];
    for (var i = 0; i < clasp.length; i++) { 
     options += '<option value="' + clasp[i] + '">' + clasp[i] + '</option>'; 
    }
    $("#leather3").html(options);
   }
 );
});