Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/75.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
我的jquery单选按钮有什么问题?_Jquery_Jquery Selectors - Fatal编程技术网

我的jquery单选按钮有什么问题?

我的jquery单选按钮有什么问题?,jquery,jquery-selectors,Jquery,Jquery Selectors,你好,我是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> <scr

你好,我是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>

jqueryhelloworld
$(“输入[@name='chkBox'])。单击(函数(){
如果($(“#chkBox”]:选中“.val()=”a“)
//处理值“a”的代码
{
$(“#msgid”).html(“这是JQuery提供的Hello World”);
}
});

脚本中有很多错误

  • 将复选框click函数包装在
    $(document).ready(function(){})
    中,否则以后将无法看到复选框被添加到DOM中
  • 请检查web浏览器的错误控制台以捕获其他错误
固定脚本:

<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>

jqueryhelloworld
$(文件)。准备好了吗(
函数(){
$(“#chkBox”)。单击(
函数(){
if($(“#chkBox:checked”).val(){
//处理值“a”的代码
$(“#msgid”).html(“这是JQuery提供的Hello World”);
}
}
);                                                                                                                                           
}                                                                                                                                            
);                                                                                                                                               

不仅将其放入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>

jqueryhelloworld
$(函数(){//on dom ready
$('input[name=chkBox]')。单击(function(){//syntax corrected
if($('#chkBox:checked').val()=='a'){//语法已更正,选择器已更正
$('#msgid').html(“这是JQuery提供的Hello World”);
}
});
});

直接在jQuery选择器中使用匿名函数是不受鼓励的。使用
$(function(){});
覆盖
$(document).ready(function(){})
是不受欢迎的,虽然不太受欢迎,但在jQuery和文档状态方面这并不是常态。这不是一个大问题,只是一个小提示。您甚至尝试过调试吗?甚至在so上突出显示的语法都表明,例如$(“#chkBox”]:checked”).val()是错误的。