如何使用 jQuery 根据单选按钮状态动态更新隐藏选择元素中的选定选项?
我在 html 页面中有 n 个选择元素,用于允许用户选择类别。
所有选定的值都需要发布到名称属性值为 category[] 的服务器(这是我正在使用的 CMS 的限制),但实际上有多个类别组用户必须从中选择一个,当输出为表单元素时,类别会分成多个选择元素,如下所示:
<select name="category[]" id="price">
<option value="1">Under £30</option>
<option value="2">£30-50</option>
<option value="3">£50-100</option>
</select>
<select name="category[]" id="colour">
<option value="4">Red</option>
<option value="5">Green</option>
<option value="6">Blue</option>
</select>
以便用户可以轻松地看到每组中可用的所有选项,我动态地将每个选择替换为使用 jQuery 1.4 的单选按钮组。我不能将它们作为单选按钮插入到 html 中,因为当我想要每组一个时,CMS 所需的相同名称 attr 只允许总共一个选择。
动态插入的单选按钮使用生成它们的选择的 id attr 值作为其名称属性值,并且与生成它们的选择选项具有完全相同的值。
为每个 select/radio 对提供如下生成的 html:
<input value="1" name="cat_price" type="radio"><label>Under £30</label>
<input value="2" name="cat_price" type="radio"><label>£30-50</label>
<input value="3" name="cat_price" type="radio"><label>£50-100</label>
<select name="category[]" id="price">
<option value="1">Under £30</option>
<option value="2">£30-50</option>
<option value="3">£50-100</option>
</select>
然后动态隐藏 select 元素,以便只有 radio 组可见。
我想要做的(并且不能完全解决)是对于每个选择元素,每当名称属性与选择的 id 相同的单选按钮的选中值发生变化时,动态更新所选选项。即,当无线电输入更改为以下内容时:
<input value="1" name="cat_price" type="radio"><label>Under £30</label>
<input value="2" name="cat_price" type="radio" checked="checked"><label>£30-50</label>
<input value="3" name="cat_price" type="radio"><label>£50-100</label>
我的(隐藏)选择更新为:
<select name="category[]" id="price">
<option value="1">Under £30</option>
<option value="2" selected="selected">£30-50</option>
<option value="3">£50-100</option>
</select>
我可以将它们发回服务器,保留原始名称属性并保留所有用户在选择中的选择。
I have n number of select elements in an html page that are used to allow the user to select categories.
All of the selected values need to get posted to the server with name attr values of category[] (this a restriction of the CMS I'm using), but as there are in fact multiple groups of categories from which the user must choose one each, when output as form elements the categories are split across multiple select elements like so:
<select name="category[]" id="price">
<option value="1">Under £30</option>
<option value="2">£30-50</option>
<option value="3">£50-100</option>
</select>
<select name="category[]" id="colour">
<option value="4">Red</option>
<option value="5">Green</option>
<option value="6">Blue</option>
</select>
So that users can easily see all of the options available in each group, I am dynamically replacing each of these selects with a group of radio buttons using jQuery 1.4. I can't just insert them into the html as radio buttons because the identical name attr required by the CMS would then only allow one selection total when I want one per group.
The dynamically inserted radio buttons use the id attr value of the select that they were generated from as their name attr value, and have exactly the same values as the select options they are generated from.
Giving us generated html like this for each select/radio pair:
<input value="1" name="cat_price" type="radio"><label>Under £30</label>
<input value="2" name="cat_price" type="radio"><label>£30-50</label>
<input value="3" name="cat_price" type="radio"><label>£50-100</label>
<select name="category[]" id="price">
<option value="1">Under £30</option>
<option value="2">£30-50</option>
<option value="3">£50-100</option>
</select>
The select element is then hidden dynamically so that only the radio group is visible.
What I want to do (and can't quite work out) is for each select element, dynamically update the selected option whenever the checked value of the radio button with a name attr identical to the id of the select changes. ie so that when the radio inputs get changed to something like:
<input value="1" name="cat_price" type="radio"><label>Under £30</label>
<input value="2" name="cat_price" type="radio" checked="checked"><label>£30-50</label>
<input value="3" name="cat_price" type="radio"><label>£50-100</label>
my (hidden) select gets updated to:
<select name="category[]" id="price">
<option value="1">Under £30</option>
<option value="2" selected="selected">£30-50</option>
<option value="3">£50-100</option>
</select>
Which I can them post back to the server, preserving the original name attr and keeping all user choices across selects.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好吧,发现这实际上并不那么棘手:
Okay, figured out this wasn't actually so tricky: