我有一个php网页表单,当用户选择一个单选按钮时,我只想在下拉列表中提供某些选项。例如,如果我从我的广播组中选择众议院,我只希望用户看到 House1 , House2 和众议院下拉菜单中有3个选项。
使用jquery可以吗?如果是这样,我将如何编码呢?
jsFiddle here: 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>
提前谢谢。
答案 0 :(得分:2)
看起来像这样:
$(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());
}
});
});
答案 1 :(得分:2)
最简单的方法是将css类分配给选择选项并显示/隐藏它们。
<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>Select</option>
<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>
<script>
jQuery(function($) {
$('input:radio').change(function(){
var val = $('input:radio:checked').val();
console.log(val)
$('#Select1').val(0)
$('option[value]').hide();
$('option[value*="-' + val + '"]').show();
});
});
</script>
答案 2 :(得分:2)
您有两个元素,其中id
为“Radio1”。你应该改变它。
代码在这里:http://jsfiddle.net/kkk7J/5/
//record of temporarily removed options
var oldoptions = [];
$("[type=radio]").on('click', function () {
//add all filtered options back
$("#Select1").append(oldoptions);
//Remove any option whose text does not contain the text of the selected
//radio button
oldoptions = $("#Select1 option:not(:contains(" + $(this).val() + "))").detach();
});
分离元素是必要的,因为并非所有浏览器都支持隐藏<option>
。此外,至少某些浏览器将保留当前选定的选项,即使它被隐藏。这些都可以通过分离轻松解决。