我希望能够使用单选按钮和 jQuery - HTML 动态更改显示的 div:
<div id="myRadioGroup">
2 Cars<input type="radio" name="cars" checked="checked" value="2" />
3 Cars<input type="radio" name="cars" value="3" />
<div id="twoCarDiv">
2 Cars Selected
</div>
<div id="threeCarDiv">
3 Cars
</div>
</div>
和 jQuery:
<script>
$(document).ready(function(){
$("input[name$='cars']").click(function() {
var test = $(this).val();
$("div.desc").hide();
$("#"+test).show();
});
});
</script>
这什么也不做,div 总是显示。我确信这很简单,但我不擅长 jQuery 。
你走在正确的道路上,但你忘记了两件事:
- add the
desc
类到你的描述div
- 您有以下数值
input
但文本为id
.
我已经修复了上面的问题,并在最初添加了一行hide()
the third描述部分
实际操作看看——http://jsfiddle.net/VgAgu/3/ http://jsfiddle.net/VgAgu/3/
HTML
<div id="myRadioGroup">
2 Cars<input type="radio" name="cars" checked="checked" value="2" />
3 Cars<input type="radio" name="cars" value="3" />
<div id="Cars2" class="desc">
2 Cars Selected
</div>
<div id="Cars3" class="desc" style="display: none;">
3 Cars
</div>
</div>
JQuery
$(document).ready(function() {
$("input[name$='cars']").click(function() {
var test = $(this).val();
$("div.desc").hide();
$("#Cars" + test).show();
});
});
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)