繁体   English   中英

有人可以告诉我错误在哪里吗? 说$未定义,预期对象

[英]Can some tell me where is the error? saying $ not defined, object expected

说$未定义,预期对象..实际上我想验证是否单击按钮时检查所有单选按钮组! 帮助PLZ

   <script type="text/javascript">
 $(document).on('click', 'form', function () {

    var validate = true;
   var unanswered = new Array();

   // Loop through available sets
   $('.qselections').each(function () {
    // Question text
    var question = $(this).prev().text();
    // Validate
    if (!$(this).find('input').is(':checked')) {
        // Didn't validate ... dispaly alert or do something
        unanswered.push(question);

        validate = false;
    }
});

if (unanswered.length > 0) {
    msg = "Please answer the following questions:\n" + unanswered.join('\n'); 
    alert(msg);
}
return validate;
});
</script>

您是否忘了在代码之前在标记中包含jquery.js文件?

设置脚本引用的一种普遍接受的方法如下(还有其他方法,这不是万能的):

<html>
    <head>
    </head>
    <body>
        <script src="http://code.jquery.com/jquery-1.11.1.min.js"></script>
        <script src="MyScriptFile.js"></script>
        <script>
            $(function() {
                $(document).on('click', 'form', function () {

                    var validate = true;
                   var unanswered = new Array();

                   // Loop through available sets
                   $('.qselections').each(function () {
                    // Question text
                    var question = $(this).prev().text();
                    // Validate
                    if (!$(this).find('input').is(':checked')) {
                        // Didn't validate ... dispaly alert or do something
                        unanswered.push(question);

                        validate = false;
                    }
                });

                if (unanswered.length > 0) {
                    msg = "Please answer the following questions:\n" + unanswered.join('\n'); 
                    alert(msg);
                }
                return validate;
            });
        </script>
    </body>
</html>

这样,您可以确定(1)在任何脚本尝试做脚本之前加载页面,以及(2)在其他可能要使用它的脚本之前包含jQuery。

这不是万无一失的,您(最终)可能需要使用Require.js之类的内容,或者作为更简单的步骤使用defer属性。

1.在head部分的first script tag中是否包含了Jquery lib。 在这种情况下,将仅执行基于jquery的代码。

2.是否wrap the code into $(document).ready()函数中。

3您是否正在使用PHP 因此需要将$替换$ Jquery.

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM