我有一个PHP web表单,当用户选择一个单选按钮时,我只想在下拉列表中提供某些选项.例如,如果我从我的广播组中选择House,我只希望用户在下拉列表中看到House1,House2和House 3选项.
这有可能使用jquery吗?如果是这样,我将如何编码?
jsFiddle这里:http://jsfiddle.net/justmelat/kkk7J/
这里的HTML:
<form method="post"> <fieldset id="Group1" name="Group1"> <legend>Group Box</legend> <input id="Radio1" name="Radio1" type="radio" value="House" />House<br /> <input id="Radio1" name="Radio1" type="radio" value="Condo" />Condo<br /> <input id="Radio1" name="Radio1" type="radio" value="Boat" />Boat<br /> </fieldset><br/><br/> <fieldset id="Group2" name="Group2"> <legend>Group Options</legend> <select id="Select1" name="Select1"> <option value="HO-House 1">House 1</option> <option value="HO-House 2">House 2</option> <option value="HO-House 3">House 3</option> <option value="CO-Condo 1">Condo 1</option> <option value="CO-Condo 2">Condo 2</option> <option value="CO-Condo 3">Condo 3</option> <option value="BO-Boat 1">Boat 1</option> <option value="BO-Boat 2">Boat 2</option> <option value="BO-Boat 3">Boat 3</option> </select></fieldset></form>
先谢谢你.
解决方法
它可能看起来像这样:
$(function(){ var select = $('#Select1'),options = select.find('option'); $('[type="radio"]').click(function(){ var visibleItems = options.filter('[value*="' + $(this).val() + '"]').show(); options.not(visibleItems).hide(); if(visibleItems.length > 0) { select.val(visibleItems.eq(0).val()); } }); });