2017-01-25 25 views
0

時,這是一個完美的控制檯工作代碼:jQuery的控制檯工作,但不加載網站

jQuery(".cf7_wrap_com li").each(function(){ 
     jQuery(this).find(".first_univer input").click(function(){ 
      var label1 = jQuery(this).parent().find("label").html();  
      if(jQuery(this).is(":checked")) { 
        jQuery(this).parent().find(".first_high").show(); 
         jQuery(".cf7_wrap_com li").not(jQuery(this).parent().parent()).each(function(){   
          jQuery(this).find(".first_high").hide();  
         }); 
          } 

      else {} 
      }); 
     }); 

和下面的代碼是什麼,我保存在我的自定義的js文件,而不是工作的網站負載

jQuery(document).ready(function($){ 
    jQuery(window).bind("load", function() { 
    jQuery(".cf7_wrap_com li").each(function(){ 
     jQuery(this).find(".first_univer input").click(function(){ 
      var label1 = jQuery(this).parent().find("label").html();  
      if(jQuery(this).is(":checked")) { 
        jQuery(this).parent().find(".first_high").show(); 
         jQuery(".cf7_wrap_com li").not(jQuery(this).parent().parent()).each(function(){   
          jQuery(this).find(".first_high").hide();  
         }); 
          } 

      else {} 
      }); 
     }); 
    }); 
}); 

我也想知道是否有可能修剪此代碼,因爲我想我可能已經添加了一些不必要的東西。

我在兩個單選按鈕(默認情況下處於非活動狀態)上使用它,當用戶單擊其中任何一個按鈕時打開一個簡短的窗體。

以下是圖像:

enter image description here

這是html

<h1>What type of student?</h1> 
<ul class="cf7_wrap_com first_use"> 
<li> 
<div class="highschoolform first_univer"><input id="#school" name="School" type="radio" value="highschool" /> <label for="highschool">High School</label> 
<div class="cf7_wrap_hs first_high">[contact-form-7 id="3552" title="High school"]</div> 
</div> 
</li> 
<li> 
<div class="unversityform first_univer"><input id="#University" name="School" type="radio" value="University" /> <label for="University">University</label> 
<div class="cf7_wrap_uni first_high">[contact-form-7 id="3553" title="University"]</div> 
</div> 
</li> 
</ul> 
+0

您是否收到任何錯誤消息在開發者控制檯?什麼是你的HTML - 你是在需要jQuery的腳本之前加載jQuery? –

+0

我剛剛發佈了HTML。我在開發中沒有收到任何錯誤消息。安慰。爲了在腳本之後加載它,我添加了加載函數(可能這是我做錯的地方) – Rahul

+0

你有包括jquery嗎? –

回答

1
  1. 給無線電一類
  2. 使用最接近和兄弟姐妹

$(function() { 
 
    $(".school").on("click",function() { 
 
    $(this).closest("ul").find(".first_high").hide(); 
 
    $(this).siblings(".first_high").show(); 
 
    }); 
 
});
.first_high { display:none }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<h1>What type of student?</h1> 
 
<ul class="cf7_wrap_com first_use"> 
 
    <li> 
 
    <div class="highschoolform first_univer"> 
 
     <input id="#school" class="school" name="School" type="radio" value="highschool" /> 
 
     <label for="highschool">High School</label> 
 
     <div class="cf7_wrap_hs first_high">[contact-form-7 id="3552" title="High school"]</div> 
 
    </div> 
 
    </li> 
 
    <li> 
 
    <div class="unversityform first_univer"> 
 
     <input id="#University" class="school" name="School" type="radio" value="University" /> 
 
     <label for="University">University</label> 
 
     <div class="cf7_wrap_uni first_high">[contact-form-7 id="3553" title="University"]</div> 
 
    </div> 
 
    </li> 
 
</ul>

+0

單選按鈕仍然無法使用。我想這是一個問題,腳本不會在我的頁面之後加載,但無法弄清楚。 http://i.imgur.com/ZnFz40t.gif – Rahul

+0

任何控制檯錯誤? – mplungjan

+0

它在控制檯中完美工作,沒有顯示任何錯誤,並且當我在將它保存在我的js文件 – Rahul

相關問題