2014-04-12 2 views
0

두 개의 드롭 다운 및 내 함수 호출 코드를 구현하고 드롭 다운에서 무언가를 선택한 후 함수가 호출되지 않습니다.드롭 다운 선택 후 함수가 호출되지 않음

코드는 드롭 다운 선택과 관련된 기능을 실행한다고 가정합니다. 이것은 정상적인 드롭 다운에서 테스트되었고 모든 것이 원활하게 실행되었지만 어떤 이유로이 두 번의 드롭 다운에서 실행되지 않습니다.

코드가 선택을 출력하고 있지만이 함수를 호출하고 싶습니다.

도움을 주시면 감사하겠습니다.

감사합니다.

 <!-- The first select list --> 
    <select name="slist1" onchange="SList.getSelect('slist2', this.value);"> 
    <option>- - -</option> 
    <option value="amazon">Amazon</option> 
    <option value="apple">Apple</option> 
    <option value="keurig">Keurig</option> 
    <option value="nike">Nike</option> 
    </select> 
    <!-- Tags for the seccond dropdown list, and for text-content --> 
    <span id="slist2"></span> <div id="scontent"></div> 

    <script><!-- 
    /* Script Double Select Dropdown List, from: coursesweb.net/javascript/ */ 
    var SList = new Object();    // JS object that stores data for options 

    // HERE replace the value with the text you want to be displayed near Select 
    var txtsl2 = ''; 

    /* 
    Property with options for the Seccond select list 
    The key in this object must be the same with the values of the options added in the first select 
    The values in the array associated to each key represent options of the seccond select 
    */ 
    SList.slist2 = { 
    "amazon": ['Kindle Fire HD', 'Kindle Charger', 'Kindle Fire HDX'], 
    "apple": ['MacBook', 'iMac', 'iPhone', 'iPad'], 
    "keurig": ['Platinum', 'Vue'], 
    "nike": ['Fuel Band'] 
    }; 


    /* 
    Property with text-content associated with the options of the 2nd select list 
    The key in this object must be the same with the values (options) added in each Array in "slist2" above 
    The values of each key represent the content displayed after the user selects an option in 2nd dropdown list 
    */ 

SList.scontent = { 
"Kindle Fire HD": 'kindlefirehd', 
"Kindle Charger": 'kindlecharg', 
"Kindle Fire HDX": 'kindlefirehdx', 
"MacBook": 'macbook', 
"iMac": 'imac', 
"iPhone": 'iphone', 
"iPad": 'ipad', 
"Platinum": 'platinum', 
"Vue": 'vue', 
"FuelBand": 'fuelband' 
}; 





     /* From here no need to modify */ 

    // function to get the dropdown list, or content 
    SList.getSelect = function(slist, option) { 
     document.getElementById('scontent').innerHTML = '';   // empty option-content 

     if(SList[slist][option]) { 
     // if option from the last Select, add text-content, else, set dropdown list 
     if(slist == 'scontent') document.getElementById('scontent').innerHTML = SList[slist][option]; 
     else if(slist == 'slist2') { 
      var addata = '<option>- - -</option>'; 
      for(var i=0; i<SList[slist][option].length; i++) { 
      addata += '<option value="'+SList[slist][option][i]+'">'+SList[slist][option][i]+'</option>'; 
      } 

      document.getElementById('slist2').innerHTML = txtsl2+' <select name="slist2" onchange="SList.getSelect(\'scontent\', this.value);">'+addata+'</select>'; 
     } 
     } 
     else if(slist == 'slist2') { 
     // empty the tag for 2nd select list 
     document.getElementById('slist2').innerHTML = ''; 
     } 
    } 

    var functions = { 
     kindlefirehd: function(){window.alert("func1 called")}, 
     kindlecharge: function(){window.alert("func1 called")}, 
     kindlefirehdx: function(){window.alert("func1 called")}, 
     macbook: function(){window.alert("func1 called")}, 
     imac: function(){window.alert("func1 called")}, 
     iphone: function(){window.alert("func1 called")}, 
     ipad: function(){window.alert("func1 called")}, 
     platinum: function(){window.alert("func1 called")}, 
     vue: function(){window.alert("func1 called")}, 
     fuelband: function(){window.alert("func1 called")} 
    } 

    $("select") 
     .change(function() { 
     var selected = $("select option:selected"); 
     functions[selected.val()](); 
     }) 
     .change(); 



    </script> 
+2

여기에 코드가 너무 많습니다. 문제를 보여주는 작은 * 예로 이것을 정리해야합니다. 당신이 당신 자신대로 오류를 찾을 수있는 좋은 기회. –

답변

0

로 교체

JS 예를 이하

var SList = new Object();    // JS object that stores data for options 
    var txtsl2 = ''; 

    SList.slist2 = { 
    "amazon": ['Kindle Fire HD', 'Kindle Charger', 'Kindle Fire HDX'], 
    "apple": ['MacBook', 'iMac', 'iPhone', 'iPad'], 
    "keurig": ['Platinum', 'Vue'], 
    "nike": ['Fuel Band'] 
    }; 

SList.scontent = { 
"Kindle Fire HD": 'kindlefirehd', 
"Kindle Charger": 'kindlecharg', 
"Kindle Fire HDX": 'kindlefirehdx', 
"MacBook": 'macbook', 
"iMac": 'imac', 
"iPhone": 'iphone', 
"iPad": 'ipad', 
"Platinum": 'platinum', 
"Vue": 'vue', 
"FuelBand": 'fuelband' 
}; 

SList.getSelect = function(slist, option) { 
     document.getElementById('scontent').innerHTML = '';   // empty option-content 

if(SList[slist][option]) { 
     // if option from the last Select, add text-content, else, set dropdown list 
     if(slist == 'scontent'){ document.getElementById('scontent').innerHTML = SList[slist][option]; 
var selected = SList[slist][option]; 
functions[selected](); 
} 
     else if(slist == 'slist2') { 
      var addata = '<option>- - -</option>'; 
      for(var i=0; i<SList[slist][option].length; i++) { 
      addata += '<option value="'+SList[slist][option][i]+'">'+SList[slist][option][i]+'</option>'; 
      } 

      document.getElementById('slist2').innerHTML = txtsl2+' <select name="slist2" onchange="SList.getSelect(\'scontent\', this.value);">'+addata+'</select>'; 
     } 
     } 
     else if(slist == 'slist2') { 
     // empty the tag for 2nd select list 
     document.getElementById('slist2').innerHTML = ''; 
     } 
    } 

    var functions = { 
     kindlefirehd: function(){window.alert("func1 called")}, 
     kindlecharge: function(){window.alert("func1 called")}, 
     kindlefirehdx: function(){window.alert("func1 called")}, 
     macbook: function(){window.alert("func1 called")}, 
     imac: function(){window.alert("func1 called")}, 
     iphone: function(){window.alert("func1 called")}, 
     ipad: function(){window.alert("func1 called")}, 
     platinum: function(){window.alert("func1 called")}, 
     vue: function(){window.alert("func1 called")}, 
     fuelband: function(){window.alert("func1 called")} 
    } 

변경

변경을 시도하는만의 조건

if(SList[slist][option]) { 
      if(slist == 'scontent'){ document.getElementById('scontent').innerHTML = SList[slist][option]; 
      var selected = SList[slist][option]; // Save Selected value to variable 
      functions[selected]();    // call function with argument 
    } 

JSfiddle

경우
1

귀하의 slist2 select 동적으로 컨텐츠를 생성한다. 이벤트 처리기를 동적 ​​콘텐츠에 연결하려면 대신 $(document).on('change', 'select', function(){})을 사용해야합니다.

functions['kindlefirehd']() 대신 functions['Kindle Fire HD']() (으)로 전화를 걸었습니다.

코드이상 제거

$("select") 
     .change(function() { 
     var selected = $("select option:selected"); 
     functions[selected.val()](); 
     }) 
     .change(); 

에서 귀하가 오른쪽을 제외한 모든 실수를하는

$("select") 
    .change(function() { 
    var selected = $("select option:selected"); 
    functions[selected.val()](); 
    }) 
    .change(); 

$(document).on('change', 'select[name=slist2]', function(){ 
    var selected = $(this).val(); 
    var funcName = SList.scontent[selected]; 
    functions[funcName](); 
}); 

See Demo