2017-02-17 3 views
0

Ich habe eine Ausgabe hier.JQuery Stütze erforderlich Fehler

Prop-Funktion: So habe ich ein Problem mit der Einstellung von zwei Optionen erforderlich. Eins funktioniert und eins nicht. .prop('required',true); Werke für #additional_here_about_other_field. Dadurch wird das Feld als erforderlich festgelegt. Aber das gleiche .prop('required',true); funktioniert nicht auf #additional_who_is_your_orthodontist_field. Ich möchte, dass dieses Feld auch benötigt wird, aber es funktioniert nicht.

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

Bitte legen zum Schnipsel –

+0

@ CarstenLøvboAndersen Ihre HTML-Code hinzugefügt html –

+0

Snippet Bitte senden Sie verschiedene Probleme in getrennten Fragen – abl

Antwort

1
  1. Sie haben eine falsche Schreibweise in Ihrem Eingabefeld additional_who_is_your_orthodontist aber Sie Abfrage sagte 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

Ja! Danke, dass du das gesehen hast –

Verwandte Themen