-1

当它在下面的代码中一直说 else { 时,我不太会遇到这种语法错误:

$(document).ready(function () {

    var courseinfo = <?php echo json_encode($courseInfo);?> ;

    $('#coursesDrop').change(function () {
        var courseId = $(this).val();

        /*
            You only need to do all of this if user selects a course, so check that first.
        */
        if (courseId !== '') {
            /*
                Iterate over courses and, if the one we want exists, populate its info.
            */
            for (var i = 0, l = courseinfo.length; i < l; i++) {
                if (courseinfo[i].CourseId == courseId) {
                    $('#currentDuration').val(courseinfo[i].Duration);
                    $('#newDuration').val(courseinfo[i].Duration);
                    $('#currentCourseId').val(courseinfo[i].CourseId);
                    $('#newCourseId').val(courseinfo[i].CourseId);

                    var text = $(this).find('option:selected').text();
                    var split = text.split(' - ');
                    $('#currentCourseNo').val(split[0]);
                    $('#currentCourseName').val(split[1]);

                    /*
                        Without this break, the loop will continue until i = l.
                        We've already found our match, no need to continue.
                    */
                    break;
                }
            }
        } else {
            $('#currentCourseNo,#currentCourseName,#currentDuration,#currentCourseId').val('');
        }
    });
});

括号似乎是正确的,但为什么我在上面的代码中会出现这个语法错误?

确切的错误说Syntaxerror: syntax error,然后在视图页面源中它只是突出显示 this else{

var courseinfo = <?php echo json_encode($courseInfo);?> ;在页面源代码中输出以下内容:

var courseinfo = [{"CourseId":1,"CourseNo":"INFO101","CourseName":"Bsc Information Communication Technology","Duration":"4"},{"CourseId":2,"CourseNo":"INFO102","CourseName":"Bsc Computing","Duration":"3\/4"},{"CourseId":8,"CourseNo":"INFO103","CourseName":"Business and Finance","Duration":"3"},{"CourseId":9,"CourseNo":"INFO107","CourseName":"Mathematics","Duration":"4"}];
4

3 回答 3

3

您应该在浏览器中打开页面,得到错误,然后查看源代码以查看浏览器的实际内容。

可能的罪魁祸首是这一行:

var courseinfo = <?php echo json_encode($courseInfo); ?> ;

三种可能:

  1. 我不是 PHP 专家,<? php(后面有空格?)真的是有效的开始标签(而不是<?php 没有空格)吗?因为如果不是,则可能是 PHP 没有处理它,因此文本被逐字发送到浏览器。这自然会成为一个问题。你声称这个空间并不真的存在。

  2. 如果不是这样,我怀疑$courseInfo在 PHP 中不是您所期望的那样,因此无法正确获得输出。您现在已经引用了输出的内容,这是一个有效的数组文字。

  3. 如果您引用的文本不在 PHP 将预处理的文件中,那将是一个问题,因为代码将被发送到浏览器。通常,Web 服务器被配置为使用 PHP 来预处理.php文件,而不是.js文件。因此,如果您引用的文本在.js文件中,它将按原样(其中包含 PHP 代码)进入浏览器,这可能不是您想要的。我猜你已经将输出引用为已处理,它必须进行预处理。

  4. 我最后的想法是,您的文本else中有问题附近的隐形字符。尝试删除整行(可能是两边的几行,以防万一)并重新输入(小心)。

于 2012-12-06T13:11:22.950 回答
3

如果评论中和 TJ Crowder 显示的行确实在您的 javascript 中,请返回您的php脚本并删除空格<? php

于 2012-12-06T13:13:12.980 回答
0

我不能确定问题所在......因为你不显示任何 html 行......但是你想试试这个代码:

$(function(){
var courseinfo = <?php echo json_encode($courseInfo);?>;
$('#coursesDrop').change(function(){
    var courseId = $(this).val();

    /*
        You only need to do all of this if user selects a course, so check that first.
    */
    if (courseId != ''){
        /*
            Iterate over courses and, if the one we want exists, populate its info.
        */
        for (i = 0;i < courseinfo.length;i++){
            if (courseinfo[i].CourseId == courseId){
                $('#currentDuration').val(courseinfo[i].Duration);
                $('#newDuration').val(courseinfo[i].Duration);
                $('#currentCourseId').val(courseinfo[i].CourseId);
                $('#newCourseId').val(courseinfo[i].CourseId);

                var text = $(this).find('option:selected').text();
                var split = text.split(' - ');
                $('#currentCourseNo').val(split[0]);
                $('#currentCourseName').val(split[1]);

                /*
                    Without this break, the loop will continue until i = l.
                    We've already found our match, no need to continue.
                */
                break;
            }
        }
    } else {
        $('#currentCourseNo,#currentCourseName,#currentDuration,#currentCourseId').val('');
    }
    });
});

如果它不起作用,我说对不起好吧....祝你好运

于 2012-12-06T13:21:53.493 回答