你好我是jquery的新手,任何人都可以告诉我我的代码有什么问题
<html>
<head>
<title>jQuery Hello World</title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
</head>
<script type="text/javascript">
$("input[@name='chkBox']").click(function(){
if ($("#chkBox"]:checked").val() == 'a')
// Code for handling value 'a'
{
$("#msgid").html("This is Hello World by JQuery");
}
});
</script>
<body>
<input type="radio" name="chkBox" id="chkBox" value="a" />
<div id="msgid">
</div>
</body>
答案 0 :(得分:1)
脚本中有很多错误。
$(document).ready( function() { } )
中包含复选框点击功能,否则以后无法看到添加到DOM的复选框。修正脚本:
<html>
<head>
<title>jQuery Hello World</title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(
function() {
$("#chkBox").click(
function() {
if ($("#chkBox:checked").val() == 'a') {
// Code for handling value 'a'
$("#msgid").html("This is Hello World by JQuery");
}
}
);
}
);
</script>
</head>
<body>
<input type="radio" name="chkBox" id="chkBox" value="a" />
<div id="msgid">
</div>
</body>
</html>
答案 1 :(得分:1)
不仅仅是将它放在onload或domready中,你的语法和jquery选择器也有错误。请参阅下面的工作固定版本:
<!doctype html>
<html>
<head>
<title>jQuery Hello World</title>
</head>
<body>
<input type="radio" name="chkBox" id="chkBox" value="a" />
<div id="msgid">
</div>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script type="text/javascript">
$(function() { //on dom ready
$('input[name=chkBox]').click(function(){ //syntax corrected
if ($('#chkBox:checked').val() == 'a') { //syntax corrected, selector corrected
$('#msgid').html("This is Hello World by JQuery");
}
});
});
</script>
</body>
</html>