2013-03-25 121 views
0

I 2小時前問了一個問題,它解決了:Previous Question SolvedHTML表單第一個下拉自動改變第二個下拉選項(續)

但現在實現了它在我的代碼不能正常工作,如:http://jsfiddle.net/7YeL6/5/

取而代之的是,當我選擇「卡車」時,僅出現車輛的下拉菜單,而不是第二個出現顏色的車輛。

,所以我想是我實現它,這裏的方式是我的代碼,如果有人可以計算出我哪裏錯了:

HTML頁面

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
    <link href='http://fonts.googleapis.com/css?family=Droid+Sans' rel='stylesheet' type='text/css'> 
    <meta charset="utf-8"> 
    <title>Add Item</title> 
    <link rel="shortcut icon" href="../style/kk-a.png" type="image/x-icon"> 
    <link rel="stylesheet" href="style_copy.css" type="text/css" media="screen" /> 
    <link href="style_menu/h_menu_style.css" media="screen" rel="stylesheet" type="text/css" /> 
    <link href="style_menu/h_menu_iconic.css" media="screen" rel="stylesheet" type="text/css" /> 
    <link href="style_menu/main_color_dropdown.css" media="screen" rel="stylesheet" type="text/css" /> 
     <script src="style_menu/h_menu_prefix-free.js"></script> 
     <script src="subcategory_auto_dropdown.js"></script> 
</head> 

<body> 
<div align="center" id="mainWrapper"> 
    <?php include_once("includes_admin/header_admin_template.php");?> 
    <div id="pageContent"><br /> 
    <div align="left" style="margin-left:30px;"><a href="inventory_list.php"> « Go to Inventory List</a></div> 
    <br /> 
    <br /> 
    <div align="left" style="margin-left:24px;">  
    <form action="inventory_list.php" enctype="multipart/form-data" name="myForm" id="myform" method="post"> 
    <table width="100%" border="0" cellspacing="0"> 
     <tr> 
     <td width="32%" colspan="1" align="left"><img src="../style/add_item.png" width="200" /></td> 
     <td width="33%" align="left">&nbsp;</td> 
     <td width="35%" align="left">&nbsp;</td> 
     </tr> 

     <tr> 
      <td colspan="3" align="left"><hr style="color:#616161"; /></td> 
     </tr> 

     <tr> 
     <td colspan="1" align="left">&nbsp;</td> 
     <td align="left"><span style="padding-bottom:10px">Category</span></td> 
     <td align="left"><span style="padding-bottom:10px">Category 2</span></td> 
     </tr> 

     <tr> 
      <td colspan="1" align="left" style="padding-bottom:10px">&nbsp;</td> 
      <td align="left" style="padding-bottom:10px"> 
        <select name="category" id="category"> 
         <option selected value="Please Select">Please Select</option>   
         <option value="Cars">Cars</option> 
         <option value="Trucks">Trucks</option> 
         <option value="Motorcycles">Motorcycles</option> 
         <option value="Boats">Boats</option> 
        </select> 
      </td> 
      <td align="left" style="padding-bottom:10px"> 
        <select name="category2" id="truck" class="second"> 
         <option value="white">white</option> 
         <option value="black">black</option>    
        </select> 

        <select name="category2" id="car" class="second"> 
         <option value="red">red</option> 
         <option value="green">green</option> 
         <option value="blue">blue</option>   
        </select> 
      </td>  
     </tr> 
     <tr> 
     <td colspan="3" align="left"><input type="submit" name="button" id="button" value="Submit +ADD"/></td> 
     </tr> 
     </table> 
    </form> 
    </div> 
    <br /> 
    <br /> 
    </div> 
    <?php include_once("includes_admin/footer_admin_template.php");?> 
</div> 
</body> 
</html> 

subcategory_auto_dropdown.js

$("#category").change(function() { 
    var str = ""; 
str = $("select#category option:selected").text(); 
    if(str == "Trucks"){ 
     $("select.second").not("#truck").hide(); 
     $("#truck").show(); 
     $("#truck").fadeIn(1000); 
    } 
    else if(str == "Cars"){ 
     $("select.second").not("#car").hide(); 
     $("#car").show(); 
     $("#car").fadeIn(1000); 
    } 

}) 

style_copy.css

/* SUBCATEGORY DROPDOWN AUTO CHANGE OPTION */ 
#category2{ 
    display: none; 
} 
.second{ 
    display: none; 

} 
+0

這對我來說工作得很好。 – Terry 2013-03-25 19:44:29

+0

當我選擇「卡車」時,會出現另一個選擇列表,並帶有黑色和白色選項。對我來說似乎沒問題。 – 2013-03-25 19:44:45

+0

您的網站上是否有JS代碼的$(document).ready()部分,或者您是否粘貼了此代碼? – 2013-03-25 19:46:57

回答

1

我在代碼中看不到包含jQuery庫。在subcategory_auto_dropdown.js

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script> 

那麼,你的代碼應該是這樣的:那麼,任何其他script標籤之前添加此行

$(document).ready(function(){ 
    //js code you already have 
}); 

這部分代碼開始jQuery代碼頁時被加載。否則,代碼不起作用。

+0

真棒的傢伙。有效!謝謝!!! – 2013-03-25 19:58:32

相關問題