[英]show / hide form fields based on a select option (dropdown)
I have a select dropdown menu with many options to choose from. 我有一个选择下拉菜单,其中有很多选项可供选择。
When a person chooses an option, I need the form fields to change according to the select menu. 当一个人选择一个选项时,我需要根据选择菜单更改表单字段。
For some reason, I am stuck. 由于某种原因,我被困住了。 I would like to blame the culprit on the fact that I have classes which are hidden in the element which should be shown.
我想责怪罪魁祸首,因为我有隐藏在应该显示的元素中的类。 The problem with that is that I cannot think of the proper way to approach this.
这样做的问题是我无法想到解决此问题的正确方法。
It is probably worth mentioning that I am using WordPress and these fields are used for extra user registration fields. 可能值得一提的是,我正在使用WordPress,这些字段用于额外的用户注册字段。
Here is a fiddle for method 1 这是方法1的小提琴
Here is a fiddle for method 2 这是方法2的小提琴
html html
<div id="reg_mem_type" class="form-row form-row-wide"> <label for="reg_mem_type">'Member Type' </label>
<select id="reg_mem_type" name="mem_type" value="'.esc_attr($_POST['mem_type']).'">
<option value="ARENA">ARENA</option>
<option value="ARO">ARO</option>
<option value="BUILD">BUILD</option>
<option value="RM">RM</option>
<option value="CLUBINS">CLUBINS</option>
<option value="AFFL">AFFL</option>
<option value="HOCKEY">HOCKEY</option>
<option value="HOCKEYA">HOCKEYA</option>
<option value="PRO">PRO</option>
<option value="SKATER">SKATER</option>
<option value="WE">WE</option>
<option value="LINS">LINS</option>
<option selected disabled value="Member">Member Type</option>
</select>
</div>
<div class="member-type bs-member-type form-row form-row-wide"> <h2>personal info</h2></div>
<div class="member-type bs-member-type form-row form-row-wide"><label for="reg_first_name">'.__('First Name', 'woocommerce').'</label>
<input type="text" class="input-text" name="first_name" id="reg_first_name" size="10" value" '.esc_attr($_POST['first_name']).'" /></div>
<div class="member-type bs-member-type form-row form-row-wide"><label for="reg_last_name">'.__('Last Name', 'woocommerce').'</label>
<input type="text" class=" input-text" name="last_name" id="reg_last_name" size="10" value" '.esc_attr($_POST['last_name']).'" /></div>
<div class="arena-member-type bs-member-type form-row form-row-wide"> <h2>company info</h2></div>
<div class="bs-member-type arena-member-type form-row form-row-wide"> <label for="reg_website">'.__('Website' , 'woocommerce').'</label>
<input type="text" class="input-text" name="website" id="reg_website" value" '.esc_attr($_POST['website']).'"/></div>
<div class="arena-member-type bs-member-type form-row form-row-wide"> <label for="reg_fax">'.__('Fax' , 'woocommerce').'</label>
<input type="text" class="input-text" name="fax_num" id="reg_fax" value" '.esc_attr($_POST['fax_num']).'"/></div>
css- method 1 CSS方法1
.member-type {
display: none;
}
.pro-member-type {
display: none;
}
.bs-member-type {
display: none;
}
.arena-member-type {
display: none;
}
.show-fields {
display:block;
}
.hidden-fields {
display:none;
}
css method2 CSS方法2
.member-type {
display: none;
}
.pro-member-type {
display: none;
}
.bs-member-type {
display: none;
}
.arena-member-type {
display: none;
}
.show-fields {
display:block;
}
.hidden-fields {
display:none;
}
js method 1 js方法1
jQuery(document).ready(function ($) {
$('select[name=mem_type]').change(function () {
// hide all optional elements
$('.member-type').css('display', 'none');
$('.arena-member-type').css('display', 'none');
$('.bs-member-type').css('display', 'none');
$('.pro-member-type').css('display', 'none');
$("select[name=mem_type] option:selected").each(function () {
if ($(this).val() == "SKATER" || "HOCKEY" || "HOCKEYA") {
$('.member-type').css('show-fields');
} else if ($(this).val() == "BUILD") {
$('.bs-member-type').addclass('show-fields');
} else if ($(this).val() == "ARENA") {
$('.arena-member-type').addclass('show-fields');
} else if ($(this).val() == "PRO") {
$('.pro-member-type').addclass('show-fields');
}
});
});
});
js method 2 js方法2
jQuery(document).ready(function ($) {
$('select[name=mem_type]').change(function () {
// hide all optional elements
$('.member-type').css('display', 'none');
$('.arena-member-type').css('display', 'none');
$('.bs-member-type').css('display', 'none');
$('.pro-member-type').css('display', 'none');
$("select[name=mem_type] option:selected").each(function () {
if ($(this).val() == "SKATER" || "HOCKEY" || "HOCKEYA") {
$('.member-type').css('display', 'block');
} else if ($(this).val() == "BUILD") {
$('.bs-member-type').css('display', 'block');
} else if ($(this).val() == "ARENA") {
$('.arena-member-type').css('display', 'block');
} else if ($(this).val() == "PRO") {
$('.pro-member-type').css('display', 'block');
}
});
});
});
I'd suggest using hide
and show
to set the display instead of using css
. 我建议使用
hide
and show
来设置显示,而不要使用css
。 Additionally there is a syntax error on the following line. 此外,下一行还存在语法错误。
if ($(this).val() == "SKATER" || "HOCKEY" || "HOCKEYA")
With the ||
与
||
you can essentially break this down into 3 separate statements. 您基本上可以将其分解为3个独立的语句。
if ($(this).val() == "SKATER")
if("HOCKEY")
if("HOCKEYA")
The 2nd and 3rd expressions are always true, you need to be comparing them to the value. 第二和第三表达式始终为真,您需要将它们与值进行比较。 (ie
if($(this).val() == "SKATER" || $(this).val() == "HOCKEY" || $(this).val() == "HOCKEYA")
). (即
if($(this).val() == "SKATER" || $(this).val() == "HOCKEY" || $(this).val() == "HOCKEYA")
)。
I updated your 2nd JSFiddle to reflect these changes. 我更新了您的第二个JSFiddle以反映这些更改。
I've updated your fiddle. 我已经更新了你的小提琴。
https://jsfiddle.net/dtrm6cpr/2/ https://jsfiddle.net/dtrm6cpr/2/
One of the problems you were having (first approach) is that 您遇到的问题(第一种方法)是
$(this).val() == "SKATER" || "HOCKEY" || "HOCKEYA"
always returns true. 始终返回true。
why not use $('.object').show();
为什么不使用
$('.object').show();
and $('.object').hide();
和
$('.object').hide();
Instead of changing the CSS you could change the hidden property of the elements that have each class, for example: 您可以更改具有每个类的元素的hidden属性,而不是更改CSS,例如:
$('.member-type').prop('hidden','true'); //to hide
$('.member-type').removeAttr('hidden'); //to show
Try this instead: 尝试以下方法:
$('select[name=mem_type]').change(function(){
var tmp = this.value;
$('.form-row').hide();
if(tmp=="SKATER" || tmp=="HOCKEY" || tmp=="HOCKEYA") {
$('.member-type').show();
} else if(tmp=="BUILD") {
$('.bs-member-type').show();
} else if(tmp=="ARENA") {
$('.arena-member-type').show();
} else if(tmp=="PRO") {
$('.pro-member-type').show();
}
});
I do not have time to fully go through your problem and I am not a WordPress person, but at a glance, I think you can do this mostly with CSS. 我没有时间完全解决您的问题,而且我不是WordPress人士,但是乍一看,我认为您可以使用CSS进行此操作。 Use the classes you have, but use a container to create a class context:
使用您拥有的类,但使用容器创建类上下文:
<div class="form-container">
<div class="member-type bs-member-type form-row form-row-wide"> <h2>personal info</h2></div>
<div class="member-type bs-member-type form-row form-row-wide"><label for="reg_first_name">'.__('First Name', 'woocommerce').'</label>
<input type="text" class="input-text" name="first_name" id="reg_first_name" size="10" value" '.esc_attr($_POST['first_name']).'" /></div>
[... code omitted for brevity ...]
</div>
Now, when the select box is changed, add a CSS class to the "form-container" Div to provide a new CSS Context: 现在,更改选择框后,将CSS类添加到“表单容器” Div中以提供新的CSS上下文:
$('select[name=mem_type]').change(function () {
var $select = $(this),
$container = $('.form-container');
$container.removeAttr('class'); // clear all classes
$container.addClass('form-container'); // add back the form-container marker class
$container.addClass($select.val()); // add the selected value as a class!
}
Now, you can have CSS that drives what is hidden and what is shown: 现在,您可以使用CSS驱动隐藏的内容和显示的内容:
/* when nothing is selected, default styles hide things */
.form-container .member-type,
.form-container .arena-member-type,
.form-container .bs-member-type,
.form-container .pro-member-type {
display: none;
}
/* Now, have new selectors define visibility based on what classes are
added when a selection is made. */
/* When the BUILD class is added to the "form-container," bs-member-types
become visible */
.form-container.BUILD .bs-member-type {
display: block;
}
/* rinse and repeat for the others */
You might choose better class names for the select since all caps classes are frowned upon, but if you need the caps elsewhere, keep 'em. 您可能会选择更好的类名,因为所有大写字母类都被忽略了,但是如果您需要其他大写字母,请保留它们。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.