0

我正在尝试在此链接上进行简单的使用

http://www.appelsiini.net/projects/chained

但第二个下拉菜单仍然显示所有选项。这是我的代码

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<script language="JavaScript" type="text/javascript" src="jquery.js"></script>
<script language="JavaScript" type="text/javascript" src="jquery.chained.js"      charset="utf-8"></script>
<script>
if (jQuery().chained) {  
alert('jQuery is loaded!');  
}
else{  
alert('jQuery is not loaded!');  
}
</script>

<select id="mark" name="mark">
<option value="">--</option>
<option value="bmw">BMW</option>
<option value="audi">Audi</option>
</select>
<select id="series" name="series">
<option value="">--</option>
<option value="series-3" class="bmw">3 series</option>
<option value="series-5" class="bmw">5 series</option>
<option value="series-6" class="bmw">6 series</option>
<option value="a3" class="audi">A3</option>
<option value="a4" class="audi">A4</option>
<option value="a5" class="audi">A5</option>
</select>

<script language="JavaScript" type="text/javascript">
$("#series").chained("#mark");
</script>

</html>
4

2 回答 2

1

Document.ready 将在 DOM 完成加载时调用方法。

将代码包装在 document.ready 函数中

$(function(){
if (jQuery().chained) {  
alert('jQuery is loaded!');  
}
else{  
alert('jQuery is not loaded!');  
}
})
于 2013-02-21T07:21:46.003 回答
0

我建议您查看是否加载了 jquery,或者尝试使用 cdn 托管的 jquery,如下所示:

 <script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>

这应该有效:

<!DOCTYPE html>
  <html>
  <head>
      <meta charset="utf-8">
      <script type="text/javascript" src="http://code.jquery.com/jquery-1.9.1.min.js">
      </script>
      <script type="text/javascript" src="jquery.chained.js" charset="utf-8"></script>
      <script>
         if (jQuery().chained) {  
              alert('jQuery is loaded!');  
         }
          else{   
             alert('jQuery is not loaded!');  
         }
      </script>
   </head>
   <body>
      <select id="mark" name="mark">
           <option value="">--</option>
           <option value="bmw">BMW</option>
           <option value="audi">Audi</option>
      </select>
      <select id="series" name="series">
           <option value="">--</option>
           <option value="series-3" class="bmw">3 series</option>
           <option value="series-5" class="bmw">5 series</option>
           <option value="series-6" class="bmw">6 series</option>
           <option value="a3" class="audi">A3</option>
           <option value="a4" class="audi">A4</option>
           <option value="a5" class="audi">A5</option>
      </select>

      <script language="JavaScript" type="text/javascript">
          $("#series").chained("#mark");
      </script>

  </body>
  </html>

您在文档中遗漏了什么</head> <body></body>,试试这个或添加这些标签并查看。

于 2013-02-21T07:40:53.577 回答