<form>
<input type='radio' name='radio_flavour' checked/>Unique flavour<br/><input class='double-flavoured' type='radio' name='radio_flavour'/>Double flavoured<br/>
<select>
<option>Select the second flavour...</option>";
foreach($connection->query($sql3) as $flavour)
{
echo "<option value='{$flavour['flavour_id']}'>{$flavour['flavour_name']}</option>";
}
echo "
</select>
</form>
我需要的是当选择了第radio_flavour
类的第二个单选按钮double-flavoured
时,其下的HTML选择将被启用,反之亦然。我正在尝试使用这个jQuery代码:
$('.double-flavoured').change(function()
{
var setE = $(this).is(':checked') ? true : false;
$(this).nextAll('select').first().attr('disabled', setE);
});
但这不起作用!
如何用jQuery做到这一点?谢谢!
答案 0 :(得分:4)
您的代码仅在您单击第二个按钮时运行,而不是在您单击第一个按钮时运行,因为它没有double-flavoured
类。您需要将处理程序附加到组中的所有按钮,然后测试所选择的按钮是否具有该类。
$(":radio[name=radio_flavour]").click(function() {
$(this).nextAll('select').attr('disabled', !$(this).hasClass("double-flavoured"));
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form>
<input type='radio' name='radio_flavour' checked/>Unique flavour
<br/>
<input class='double-flavoured' type='radio' name='radio_flavour' />Double flavoured
<br/>
<select disabled>
<option>Select the second flavour...</option>";
<option value="1">Flavour 1</option>
<option value="2">Flavour 2</option>
<option value="3">Flavour 3</option>
</select>
</form>
&#13;