我正在使用jQuery 1.5.2来操作表单上的两个下拉选择框。我的一个特殊要求是,如果选择了“其他”选项,则两个选择框都必须选择“其他”。
问题:
我的代码看起来像这样:
$("#Select_One")
.change(function() {
if($(this).val() == "Other") {
$("#Select_Two").val("Other");
}
else {
if($("#Select_Two").val() == "Other") {
$("#Select_Two").val("");
}
}
});
$("#Select_Two")
.change(function() {
if($(this).val() == "Other") {
$("#Select_One").val("Other");
}
else {
if($("#Select_One").val() == "Other") {
$("#Select_One").val("");
}
}
});
问题在于,
$("#Select_Two").val("Other");
并没有强制执行两个字段都为“其他”的规则,而是将#Select_Two
下每个<option>
的value="Other"
属性添加或覆盖。#Select_One
同样存在相同的问题。此外,我的HTML来自Grails模板。 <g:select id="Select_One"
name="Units_One"
class="Required DropDown"
from="${Unit_One.values()}"
optionKey="value_"
optionValue="${{it.value_}}"
noSelection="${['': message(code: 'units.no.selection')]}"
/>
<g:select id="Select_Two"
name="Units_Two"
class="Required DropDown"
from="${Unit_Two.values()}"
optionKey="value_"
optionValue="${{it.value_}}"
noSelection="${['': message(code: 'units.no.selection')]}"
/>
在将以太网字段设置为“其他”后,Chrome调试器显示给我的内容如下...
<select name="Units_Two" id='Select_Two" class="Required DropDown">
<option value="Other">Select Number</option>
<option value="Other">Other</option>
<option value="Other">1</option>
etc...
这些函数的else子句的工作方式与预期的完全相同。
问题:
如何强制选择一个字段中的“其他”自动选择相应字段中的“其他”?