所以我现在已经乱搞了这个jQuery代码几天了,我无法让它正常工作。
基本上我的目标是拥有2组与动态div相对应的单选按钮。我希望在单击任一组单选按钮时激活jquery,接收两组按钮的值,然后根据butttons的值动态更改显示哪个div。我非常接近但我的问题是我只获得了我最近选择的按钮的值,另一个显示为未定义。
我尝试在这个问题中实现该技术,但仍然没有得到第二个变量:How to validate two sets of radio buttons
这是我的代码:
<html>
<head>
<link type="text/css" rel="stylesheet" href="JS-and-CSS/stylesheet.css" />
<script type="text/javascript" language="javascript"
src="JS-and-CSS/jquery.min.js"></script>
<title></title>
</head>
<body>
<div id="wrapper2">
<script>
$(document).ready(function() {
$("input[name$='monitorVersion']").click(function() {
var monitor = $('#monitorSelection > input:radio:checked').val();
var option = $('#optionSelection > input:radio:checked').val();
$("div.version").hide();
$("#stuff").html(monitor + option);
$("#View" + monitor + option).show();
});
$("input[name$='viewOptions']").click(function() {
var monitor = $('#monitorSelection > input:radio:checked').val();
var option = $('#optionSelection > input:radio:checked').val();
$("div.version").hide();
$("#stuff").html(monitor + option);
$("#View" + monitor + option).show();
});
});
</script>
<div id="userBox">
<div id="monitorSelection">
<h4>
label<input type="radio" name="monitorVersion" value="A"
checked="checked" /> label<input type="radio"
name="monitorVersion" value="B" /> label<input
type="radio" name="monitorVersion" value="C" />
</h4>
</div>
<div id="optionSelection">
label<input type="radio" name="viewOptions" checked="checked"
value="1" /> label<input type="radio" name="viewOptions"
value="2" /> label<input type="radio" name="viewOptions"
value="3" /> label<input type="radio" name="viewOptions" value="4" />
</div>
<div id="stuff"></div>
<div id="ViewA1" class="version">
</div>
<div id="ViewA2" class="version" style="display: none;">
</div>
<div id="ViewA3" class="version" style="display: none;">
</div>
<div id="ViewA4" class="version" style="display: none;">
</div>
<div id="ViewB1" class="version" style="display: none;">
</div>
<div id="ViewB2" class="version" style="display: none;">
</div>
<div id="ViewB3" class="version" style="display: none;">
</div>
<div id="ViewB4" class="version" style="display: none;">
</div>
<div id="ViewC1" class="version" style="display: none;">
</div>
<div id="ViewC2" class="version" style="display: none;">
</div>
<div id="ViewC3" class="version" style="display: none;">
</div>
<div id="ViewC4" class="version" style="display: none;">
</div>
</div>
</div>
</body>
</html>
答案 0 :(得分:1)
“&gt;”仅适用于直系后代。您需要将其更改为“.find”以更深入。请在此处查看:http://jsfiddle.net/6qzmL6rb/3
$(document).ready(function() {
$("input[name$='monitorVersion']").click(function() {
var monitor = $('#monitorSelection').find('input:radio:checked').val();
var option = $('#optionSelection > input:radio:checked').val();
$("div.version").hide();
$("#stuff").html(monitor + option);
$("#View" + monitor + option).show();
});
$("input[name$='viewOptions']").click(function() {
var monitor = $('#monitorSelection').find('input:radio:checked').val();
var option = $('#optionSelection > input:radio:checked').val();
$("div.version").hide();
$("#stuff").html(monitor + option);
$("#View" + monitor + option).show();
});
});