2011-12-19 69 views
0

我有一個表格,顯示和隱藏某些字段依賴於什麼樣的價值某一領域有,做這個是下面的JavaScript,值不顯示,即使是在HTML

jQuery(function(){ 

    jQuery('.voice_over, .commercial').hide(); 

    if(jQuery("#type option:selected").val() == "voice over") 
    { 
     jQuery('.voice_over, .role').fadeIn(500); 
     jQuery('.generic, .fieldError, .commercial').fadeOut(500).val(''); 
    } 

    if(jQuery("#type option:selected").val() == "television" || jQuery("#type").val() == "theatre" || jQuery("#type").val() == "radio" || jQuery("#type").val() == "film" || jQuery("#type").val() == "commercial") 
    { 
     jQuery('.voice_over, .commercial').fadeOut(500).val(''); 
     jQuery('.generic, .role').fadeIn(500); 
    } 

    if(jQuery("#type option:selected").val() == "commercial") { 
     jQuery('input[name="production_role"], .voice_over, .generic, .role').fadeOut(500).val(''); 
     jQuery('.commercial').fadeIn(500); 
    } 

    jQuery("#type").change(function(){ 

     if(jQuery(this).val() == "voice over") 
     { 
      jQuery('.generic, .commercial, .fieldError').fadeOut(500).val(''); 
      jQuery('.voice_over').fadeIn(500); 
     } 
     else if(jQuery(this).val() == "commercial") { 
      jQuery('input[name="production_role"], .role, .generic, .fieldError').fadeOut(500).val(''); 
      jQuery('.commercial').fadeIn(500); 
     } 
     else if(jQuery(this).val() == "television" || jQuery(this).val() == "theatre" || jQuery(this).val() == "radio" || jQuery(this).val() == "film") 
     { 
      jQuery('.commercial, .fieldError, .voice_over').fadeOut(500).val(''); 
      jQuery('.generic, .role, input[name="production_role"]').fadeIn(500); 
     } 

    }); 
}); 

的HTML,我得到的是如下(這是通過PHP產生),

<form action="http://urbantalent.factoryagency.co.uk/admin/candidates/add_step_3/id/177/edit" method="post" accept-charset="utf-8" class="step_3"> 
       <label for="type">Production Type</label> 
<select name="production_type" id="type"> 
<option value="television">Television</option> 
<option value="theatre">Theatre</option> 
<option value="film">Film</option> 
<option value="commercial" selected="selected">Commercial</option> 
<option value="voice over">Voice Over</option> 
<option value="short film">Short Film</option> 

<option value="corporate">Corporate/Training</option> 
<option value="radio">Radio</option> 
</select> 
       <label style="display: none;" class="generic">Production Title</label> 
     <input style="display: none;" name="production_title" value="" class="generic" type="text"> 
       <label style="display: none;" class="voice_over">Project Type</label> 
     <input style="display: none;" name="production_project_type" value="" class="voice_over" type="text"> 
       <label style="display: none;" class="voice_over">Name/Product</label> 

     <input style="display: none;" name="production_product" value="" class="voice_over" type="text"> 
       <label style="display: none;" class="voice_over">Production Agency</label> 
     <input style="display: none;" name="production_agency" value="" class="voice_over" type="text"> 
       <label style="display: none;" class="role">Role</label> 
     <input style="display: none;" name="production_role" value="" type="text"> 
       <label style="display: none;" class="generic">Director</label> 
     <input style="display: none;" name="production_director" value="" class="generic" type="text"> 

       <label style="display: none;" class="generic">Production Company</label> 
     <input style="display: none;" name="production_company" value="" class="generic" type="text"> 
       <label style="display: block;" class="commercial">Commercial Details</label> 
     <input style="display: inline;" name="production_details" value="Walking down the street looking happy - Daz (2010)" class="commercial" type="text"> 
       <input name="career_overview_id" value="177" type="hidden"> 
       <input name="candidate_id" value="" type="hidden"> 
       <input name="form_step" value="3" type="hidden"> 
        <input name="step_3" value="Save" type="submit"> 

      </form> 

正如你可以看到production_details字段的值,但我看不到任何在該領域的自我。

當我刪除javascript時,我看到這個值很好,所以我只能假設有一個錯誤,但是我看不到它。

+0

請一個http://jsfiddle.net我們。 – noob 2011-12-19 14:09:14

+1

http://jsfiddle.net/Qv54H/這將刪除'$('。generic,.fieldError,.commercial')。fadeOut(500).val('');' – mplungjan 2011-12-19 14:10:48

+0

請嘗試發佈一個最小值顯示問題的測試用例。這將更容易看到發生了什麼事情,結果你更有可能得到一個好的答案。 – 2011-12-19 14:11:45

回答

2
if(jQuery("#type option:selected").val() == "television" || jQuery("#type").val() == "theatre" || jQuery("#type").val() == "radio" || jQuery("#type").val() == "film" || jQuery("#type").val() == "commercial") 
    { 
     jQuery('.voice_over, .commercial').fadeOut(500).val(''); 
     jQuery('.generic, .role').fadeIn(500); 
    } 

您正在將val設置爲空字符串。

最重要的部分是:

if(... jQuery("#type").val() == "commercial"){ 
    jQuery('..., .commercial')...val(''); 
} 
+0

我應該回答,而不是評論:| – mplungjan 2011-12-19 14:18:23

+0

是的,但我沒有看到你的評論,所以我想我可能已經打了你幾秒鐘。至少在同一時間。 – 2011-12-19 14:19:18