jQuery显示/隐藏选项选择字段显示框,我试图显示在所有其他选项
jQuery show/hide on option select field showing the box I am trying to show on all other options
我有这个jQuery脚本:
// sorting the names of the fields we're hiding
var timeFieldArray = ["reserveTimeOfDay","choice1TimeOfDay","choice2TimeOfDay","choice3TimeOfDay"];
// show/hide an optional field for the time of day
$.each(timeFieldArray, function(index, fieldName)
{ // Start by hiding everything
$("#"+ fieldName + "Optional").hide();
$(".toValidate select[name='"+fieldName+"']").click(function() {
// is it selected & the correct value?
if($("option[value='OtherField']").is(":selected")){
$("#"+ fieldName + "Optional").show("fast");
$("input[name='"+fieldName+"Optional']").addClass("required");
} else {
$("input[name='"+fieldName+"Optional']").removeClass("required");
$("#"+ fieldName + "Optional").hide();
}
}); // closing .Click
}); // closing .Each
基本上,当Other选项被选中时,我想从timeFieldArray中获取正确的位置,并显示与该字段相关的框。问题是,当我选择另一个选项字段,而不是OtherFieldvalue这些仍然显示。有什么建议吗?
My HTML:
<ul>
<li>
<p><strong>First Choice</strong></p>
<p><label for="choice1Date">Date</label><input type="" name="choice1Date" class="dateField required" /></p>
<label for="choice1TimeOfDay">Time Of Day</label>
<select name="choice1TimeOfDay">
<option value="am"> Morning (A.M.)</option>
<option value="pm"> Afternoon (P.M.)</option>
<option value="OtherField"> Other </option>
</select>
<div id="choice1TimeOfDayOptional">
Other: <input type="text" name="choice1TimeOfDayOptional" />
</div>
</li>
<li>
<p><strong>Second Choice</strong></p>
<p><label for="choice2Date">Date</label><input type="" name="choice2Date" class="dateField required" /></p>
<label for="choice2TimeOfDay">Time OF Day</label>
<select name="choice2TimeOfDay">
<option value="am"> Morning (A.M.)</option>
<option value="pm"> Afternoon (P.M.)</option>
<option value="OtherField"> Other </option>
</select>
<div id="choice2TimeOfDayOptional">
Other: <input type="text" name="choice2TimeOfDayOptional" />
</div>
</li>
<li>
<p><strong>Third Choice</strong></p>
<p><label for="choice3Date">Date</label><input type="" name="choice3Date" class="dateField required" /></p>
<label for="choice3TimeOfDay">Time Of Day</label>
<select name="choice3TimeOfDay">
<option value="am"> Morning (A.M.)</option>
<option value="pm"> Afternoon (P.M.)</option>
<option value="OtherField"> Other </option>
</select>
<div id="choice3TimeOfDayOptional">
Other: <input type="text" name="choice3TimeOfDayOptional" />
</div>
</li>
</ul>
您正在检查一些 option
与所选择的其他值。相反,您应该检查当前选择框是否选中了Other值。
:
if($(this).find("option[value='OtherField']").is(":selected")) {
// find selected option within the select box that's changing
我也不确定,但在Chrome上我不得不使用.change
而不是.click
。
最后,就我个人而言,当淡入时,如果你也淡入,它可能会提供更好的UI体验-现在它立即消失。只是一个想法:)
http://jsfiddle.net/Dd2YM/1/相关文章:
- 从服务器获取数据并在选择控件中使用ng选项无法显示选项
- 在iPad上,如何通过Javascript事件在选择元素上设置focus()而不显示选项
- Javascript测试-使用Array.prototype以随机顺序显示选项
- 显示选项卡后向上滚动
- 使用 jQuery UI 选择菜单中选择中的隐藏/显示选项
- 无法使用 jquery 显示选项卡内容
- 如何在 angularjs 材料设计中显示选项卡标题中心对齐
- JavaScript 选择列表,循环显示选项
- 我制作了自己的jquery选项卡版本,但我需要外部链接来显示选项卡内容和样式原始导航
- 寻找跨浏览器解决方案来突出显示选项卡
- 请求方法显示选项而不是开机自检
- 如何在“挖空.js”中单击后显示选项默认标题
- 有条件地在带有角度的选择中显示选项
- 如何使用 Twitter Bootstrap 3 显示选项卡内容
- 高图表折线图中的单击并保持突出显示选项
- 火狐扩展 - >获取显示选项卡的内容
- 使用 javascript 在下拉列表中显示选项的键和值
- AngularJS Autocomplete未显示选项
- 我希望根据使用jquery单击的选项卡来显示选项卡内容
- jQuery在页面刷新时选择排序更改显示选项.什么'正在发生