我尝试在 jsfiddle.net 中运行它并且工作正常。http://jsfiddle.net/AdTK3/
HTML
<input name="rdoNationality" class="rdoNationality" type="radio" checked="checked" value="nationalityAny" /> Div 1
<input name="rdoNationality" class="rdoNationality" type="radio" value="nationalitySelect" /> Div 2
<div id="nationalityAny">
Div 1 Selected
</div>
<div id="nationalitySelect" style="display:none;">
Div 2 Selected
</div>
jQuery
$(function(){
$(".rdoNationality").on("click", function(){
var valNationality = $(this).val(),
divNationality = $("#" + valNationality);
$("div").hide();
divNationality.show();
});
});
但是当我在我的页面上尝试它时它不起作用(注意:我确定我的 jQuery 路径是正确的)。这是我的完整 HTML
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Title Here</title>
<meta name="keywords" content="kw here" />
<meta name="content-language" content="en" />
<meta http-equiv="content-type" content="text/html; charset=ISO-8859-1" />
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript">
$(function(){
$(".rdoNationality").on("click", function(){
var valNationality = $(this).val(),
divNationality = $("#" + valNationality);
$("div").hide();
divNationality.show();
});
});
</script>
</head>
<body style="background:#fff;">
<div>
<input name="rdoNationality" class="rdoNationality" type="radio" checked="checked" value="nationalityAny" /> Div 1
<input name="rdoNationality" class="rdoNationality" type="radio" value="nationalitySelect" /> Div 2
<div id="nationalityAny">
Div 1 Selected
</div>
<div id="nationalitySelect" style="display:none;">
Div 2 Selected
</div>
</div>
</body>
</html>
我的代码有什么问题。谢谢。