2017-02-17 20 views
0

我在這裏有一個問題。jquery道具所需的錯誤

支柱功能: 所以我有一個問題,將兩個選項設置爲required。一個工作,一個不工作。 .prop('required',true);適用於#additional_here_about_other_field。這將該字段設置爲必需。但.prop('required',true);不適用於#additional_who_is_your_orthodontist_field。我想要這個字段也是必需的,但它不起作用。

jQuery(document).ready(function() { 
 
\t 
 
\t if(jQuery("#additional_here_about_other_field").length > 0){ 
 
\t \t jQuery("#additional_here_about_other_field").hide(); 
 
\t \t jQuery("#additional_how_did_u_hear_about_harp").change(function(){ 
 
\t \t \t if(jQuery(this).val() == 'Other (please specify)'){ jQuery("#additional_here_about_other_field").show().prop('required',true); } 
 
\t \t \t else { jQuery("#additional_here_about_other_field").hide(); } 
 
\t \t }); 
 
\t } 
 
\t if(jQuery("#additional_who_is_your_orthodontist_field").length > 0){ 
 
\t \t jQuery("#additional_who_is_your_orthodontist_field").hide(); 
 
\t \t jQuery("#additional_how_did_u_hear_about_harp").change(function(){ 
 
\t \t \t if(jQuery(this).val() == 'Orthodontist Referral'){ jQuery("#additional_who_is_your_orthodontist_field").show().prop('required',true); } 
 
\t \t \t else { jQuery("#additional_who_is_your_orthodontist_field").hide(); } 
 
\t \t }); 
 
\t } 
 
});

HTML snippet 

<select name="additional_how_did_u_hear_about_harp" id="additional_how_did_u_hear_about_harp" class="select " data-allow_clear="true" data-placeholder="How Did You Hear About The Harp?" > 
 
\t <option value="" selected='selected'></option> 
 
\t <option value="Patient" >Patient</option> 
 
\t <option value="Orthodontist Referral" >Orthodontist Referral</option> 
 
\t <option value="Trade Show" >Trade Show</option> 
 
\t <option value="Mailer" >Mailer</option> 
 
\t <option value="Other (please specify)" >Other (please specify)</option> 
 
</select> 
 

 
<div class="clear"></div> 
 
<p> \t \t 
 
\t <input type="text" class="input-text " name="additional_here_about_other_field" id="additional_here_about_other_field" placeholder="Other (please specify)" value="" /> 
 
</p> 
 
<div class="clear"></div> 
 
<p> \t \t 
 
\t <input type="text" class="input-text " name="additional_who_is_your_orthodontist" id="additional_who_is_your_orthodontist" placeholder="Who is your orthodontist?" value="" /> 
 
</p> 
 
<div class="clear"></div>

+2

請添加HTML代碼到您的片段 –

+0

@CarstenLøvboAndersen添加HTML代碼段 –

+0

請張貼在不同的問題,不同的問題 – abl

回答

1
  1. 你有拼寫錯誤在你輸入字段additional_who_is_your_orthodontist但你查詢說additional_who_is_your_orthodontist_field

$(document).ready(function() { 
 

 
    if ($("#additional_here_about_other_field").length > 0) { 
 
    $("#additional_here_about_other_field").hide(); 
 
    $("#additional_how_did_u_hear_about_harp").change(function() { 
 
     if ($(this).val() == 'Other (please specify)') { 
 
     $("#additional_here_about_other_field").show().prop('required', true); 
 
     } else { 
 
     $("#additional_here_about_other_field").hide(); 
 
     } 
 
    }); 
 
    }; 
 
    if ($("#additional_who_is_your_orthodontist").length > 0) { 
 
    $("#additional_who_is_your_orthodontist").hide(); 
 
    $("#additional_how_did_u_hear_about_harp").change(function() { 
 
     if ($(this).val() == 'Orthodontist Referral') { 
 
     $("#additional_who_is_your_orthodontist").show().attr('required', true); 
 
     } else { 
 
     $("#additional_who_is_your_orthodontist").hide(); 
 
     } 
 
    }); 
 
    } 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<select name="additional_how_did_u_hear_about_harp" id="additional_how_did_u_hear_about_harp" class="select " data-allow_clear="true" data-placeholder="How Did You Hear About The Harp?"> 
 
\t <option value="" selected='selected'></option> 
 
\t <option value="Patient" >Patient</option> 
 
\t <option value="Orthodontist Referral" >Orthodontist Referral</option> 
 
\t <option value="Trade Show" >Trade Show</option> 
 
\t <option value="Mailer" >Mailer</option> 
 
\t <option value="Other (please specify)" >Other (please specify)</option> 
 
</select> 
 

 
<div class="clear"></div> 
 
<p> 
 
    <input type="text" class="input-text" name="additional_here_about_other_field" id="additional_here_about_other_field" placeholder="Other (please specify)" value="" /> 
 
</p> 
 
<div class="clear"></div> 
 
<p> 
 
    <input type="text" class="input-text" name="additional_who_is_your_orthodontist" id="additional_who_is_your_orthodontist" placeholder="Who is your orthodontist?" value="" /> 
 
</p> 
 
<div class="clear"></div>

+0

是的!感謝您的發現 –