2017-08-08 74 views
0

我正在創建一個Dropbox來創建圖表。我一直在使用函數change()從組合框中獲取文本值。這是代碼:組合框代碼的錯誤是什麼?

\t \t 
 
\t \t //Load the Visualization API and the chart package. 
 
\t \t google.charts.load('current', {'packages':['corechart']}); 
 
\t \t //google.load('visualization', '1', {'packages':['corechart','table']}); 
 

 
\t \t //get the selected value 
 
\t \t function change() { 
 
\t \t \t var listbox = document.getElementById("chart"); 
 
\t \t \t var selIndex = listbox.seletedIndex; 
 
\t \t \t var selValue = listbox.options[selIndex].value; 
 
\t \t \t var selText = listbox.options[selIndex].text; 
 
\t \t \t 
 
\t \t \t //console.log(selValue); 
 
\t \t 
 

 
\t \t //chart for apply job post 
 
\t \t google.charts.setOnLoadCallback(dashboardDraw); 
 

 
\t \t function dashboardDraw(x, y){ 
 

 
\t \t \t //get the location data from table 
 
\t \t \t var jsonLocationData = $.ajax({ 
 
\t \t \t \t \t url: "post_location.php", 
 
\t \t \t \t \t data: '{}', 
 
\t \t \t \t \t dataType: "json", 
 
\t \t \t \t \t async: false 
 
\t \t \t \t \t }).responseText; 
 

 
\t \t \t //create our data table out of json data loaded from server 
 
\t \t \t var LocationData = new google.visualization.DataTable(jsonLocationData); 
 

 
\t \t \t //get the company data from table 
 
\t \t \t var jsonCompanyData = $.ajax({ 
 
\t \t \t \t \t url: "post_company.php", 
 
\t \t \t \t \t data: '{}', 
 
\t \t \t \t \t dataType: "json", 
 
\t \t \t \t \t async: false 
 
\t \t \t \t \t }).responseText; 
 

 
\t \t \t //create our data table out of json data loaded from server 
 
\t \t \t var CompanyData = new google.visualization.DataTable(jsonCompanyData); 
 
\t \t \t 
 
\t \t \t 
 
\t \t \t //company chart details 
 
\t \t \t var optionsLocation = { 
 
     \t \t \t title: 'Job Posts by Location', 
 
     \t \t \t pieSliceText: 'label', 
 
     \t \t \t tooltip: {isHtml: true}, 
 
     \t \t \t width: 700, 
 
     \t \t \t height: 500, 
 
     \t \t \t chartArea: { left:"5%",top:"5%",width:"90%",height:"90%" } 
 
     \t \t }; 
 
\t \t \t 
 

 
     \t \t //company chart details 
 
     \t \t var optionsCompany = { 
 
     \t \t \t title: 'Job Posts by Company', 
 
     \t \t \t pieSliceText: 'label', 
 
     \t \t \t tooltip: {isHtml: true}, 
 
     \t \t \t width: 700, 
 
     \t \t \t height: 500, 
 
     \t \t \t chartArea: { left:"5%",top:"5%",width:"90%",height:"90%" } 
 
     \t \t }; 
 
\t \t \t 
 
     \t \t var chart = new google.visualization.PieChart(document.getElementById('chart_div')); 
 
     \t \t \t if (selValue == 'location_val') { 
 
     \t \t \t x = LocationData; 
 
     \t \t \t y = optionsLocation; 
 
     \t \t \t } 
 
     
 
     \t \t \t if (selValue == 'company_val') { 
 
     \t \t \t x = CompanyData; 
 
     \t \t \t \t y = optionsCompany; 
 
     \t \t \t } 
 
     
 
     \t chart.draw(x, y); 
 
    \t } 
 
    \t \t 
 
     
 

 
    \t \t \t \t 
 
\t } \t
\t <script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script> 
 
\t <script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script> 
 

 

 
    
 

 

 
<table> 
 
    <th> 
 
     <select id="chart" name="select1" onchange="change()"> 
 
     \t <option selected disabled="select">Select</option> 
 
     \t <option value="company_val">By Company</option> 
 
     \t <option value="location_val">By Location</option> 
 
     </select> 
 
    </th> 
 
</table> 
 
    <div id="chart_div"></div>

控制檯顯示以下錯誤:

TypeError: listbox.options[selIndex] is undefined[Learn More] dashboard.php:25:8

代碼的問題一部分是這樣的:

var selValue = listbox.options[selIndex].value; 
    var selText = listbox.options[selIndex].text; 

哪有我解決了這個問題?我檢查了代碼,沒有語法問題。

+0

您是否檢查過'selIndex'實際包含的值? – CBroe

+0

值是undefined – joun

回答

1

您是否嘗試過使用您的調試器?

如果您使用內置在瀏覽器中的調試工具並在錯誤發生的行之前設置斷點,您將看到selIndex未定義。所以你會看看分配一個值的那一行。

var selIndex = listbox.seletedIndex; 

你的錯誤是在該行,使用調試器,你可以看到,包括selectedIndex注意c在列表框中的可用屬性。

+0

即時通訊使用mozila調試器控制檯,它顯示上述錯誤 – joun

+0

他的意思是改變'seletedIndex'到'selectedIndex' – ewwink

+0

owh ..感謝你..它解決了這個問題。有時在檢查我們自己的代碼時,通常這個問題不能被看到,儘管它是一個明顯錯誤的類型代碼。 – joun