最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - An invalid form control with name...is not focusable - Stack Overflow

programmeradmin1浏览0评论

An invalid form control with name='additional_here_about_other_field' is not focusable. This code is for a select field with four dropdowns. A couple of the options are to be required: #additional_here_about_other_field and the #additional_who_is_your_orthodontist_field. When a required field is selected, you have to enter additional data in another text field that will show/unhide. When you select a required option and then switch to an option which is required or not and try to submit the form, you get the is not focusable error. It seems when you select a required field and then switch to another field, the previous required field although now hidden is still waiting to be validated?

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

An invalid form control with name='additional_here_about_other_field' is not focusable. This code is for a select field with four dropdowns. A couple of the options are to be required: #additional_here_about_other_field and the #additional_who_is_your_orthodontist_field. When a required field is selected, you have to enter additional data in another text field that will show/unhide. When you select a required option and then switch to an option which is required or not and try to submit the form, you get the is not focusable error. It seems when you select a required field and then switch to another field, the previous required field although now hidden is still waiting to be validated?

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

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?" >
	<option value=""  selected='selected'></option>
	<option value="Patient" >Patient</option>
	<option value="Orthodontist Referral" >Orthodontist Referral</option>
	<option value="Trade Show" >Trade Show</option>
	<option value="Mailer" >Mailer</option>
	<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>

Share Improve this question asked Feb 17, 2017 at 17:54 Vim BonsuVim Bonsu 1,8926 gold badges21 silver badges28 bronze badges 1
  • for those finding this error message search result with custom elements and form participation there may be a solution at stackoverflow.com/a/69684377 – jimmont Commented Oct 23, 2021 at 1:37
Add a comment  | 

3 Answers 3

Reset to default 31

Just because a form control is hidden doesn't mean it isn't required. And since it is required, but hidden the browser can't focus the form control.

Every place you have .hide() change it to .hide().prop('required',false) to fix your problem.

Remove "required" from the field, and use a javascript logic to see if it has been required or not.

First, remove "required" from the field. Then use this jQuery code:

$('#your_input_or_select_id').prop('required',true);   //use this to add required while document ready
$('#your_input_or_select_id').removeAttr('required'); //use this to remove required whenever you want
发布评论

评论列表(0)

  1. 暂无评论