在下拉菜单中未选择任何选项

时间:2012-12-02 23:24:41

标签: php javascript jquery html

我有一个申请here

现在让我们在我的数据库中说它看起来像这样:

课程表:

CourseId CourseNo CourseName          Duration
1        INFO101  ICT                 3/4
2        INFO102  Computing           2/3
3        INFO103  Computing Science   3

在课程下拉菜单中,它显示数据库中的couses列表,如下所示:

Please Select
INFO101-ICT 
INFO102-Computing
INFO103-Computing Science 

在持续时间下拉菜单中,它会显示以下持续时间选项:

Please Select
1
1/2
2
2/3
3
3/4
4
4/5
5
5/6
6
6/7
7
7/8
8
8/9
9
9/10
10

现在在应用程序中发生的事情是,当您从下拉菜单中选择一个课程时,它应该从持续时间下拉菜单中选择duartion,该菜单与属于数据库中课程的持续时间相匹配。

例如,如果我从coure下拉菜单中选择课程INFO101-ICT,那么它应该会在持续时间下拉菜单中自动选择持续时间3/4

但是在应用程序中没有这样做,当我选择一个课程时,只需在持续时间下拉菜单中选择一个空白选项。

我的问题是如何在持续时间下拉菜单中选择正确的持续时间,具体取决于从课程下拉菜单中选择的课程?

以下是该应用程序的完整代码:

     <?php

        // connect to the database
        include('connect.php');


        /* check connection */
        if (mysqli_connect_errno()) {
            printf("Connect failed: %s\n", mysqli_connect_error());
            die();
        }

        ?>

        <h1>EDIT COURSE</h1>

        <?php

        $min_year     = 1;
        $max_year     = 10;
        $years        = range($min_year, $max_year); // returns array with numeric values of 1900 - 2012
        $durationHTML = '';
        $durationHTML .= '<select name="duration" id="newDuration">' . PHP_EOL;
        $durationHTML .= '<option value="">Please Select</option>' . PHP_EOL;

        foreach ($years as $year) {
            $durationHTML .= "<option>$year</option>" . PHP_EOL;
            if ($year != $max_year) {
                $nextYear = $year + 1;
                $durationHTML .= "<option>$year/$nextYear</option>" . PHP_EOL;
            }
        }
        $durationHTML .= '</select>';

        $newCourseNo = (isset($_POST['CourseNoNew'])) ? $_POST['CourseNoNew'] : '';

        $coursequery = "
        SELECT CourseId, CourseNo, CourseName, Duration
        FROM Course
        ORDER BY CourseNo
        ";

        $courseqrystmt = $mysqli->prepare($coursequery);
        // You only need to call bind_param once

        $courseqrystmt->execute();

        $courseqrystmt->bind_result($dbCourseId, $dbCourseNo, $dbCourseName, $dbDuration);

        $courseqrystmt->store_result();

        $coursenum = $courseqrystmt->num_rows();

        $courseHTML = '';

        $courseHTML = '<select name="courses" id="coursesDrop">' . PHP_EOL;
        $courseHTML .= '<option value="">Please Select</option>' . PHP_EOL;

        $courseInfo = array();

        while ($courseqrystmt->fetch()) {
            $courseHTML .= sprintf("<option value='%s'>%s - %s</option>", $dbCourseId, $dbCourseNo, $dbCourseName) . PHP_EOL;

            $courseData               = array();
            $courseData["CourseId"]   = $dbCourseId;
            $courseData["CourseNo"]   = $dbCourseNo;
            $courseData["CourseName"] = $dbCourseName;
            $courseData["Duration"]   = $dbDuration;

            array_push($courseInfo, $courseData);
        }


        $courseHTML .= '</select>';


        $courseform = "
        <form action='" . htmlentities($_SERVER['PHP_SELF']) . "' method='post' id='courseForm'>
        <p><strong>Course:</strong> {$courseHTML} </p>   
        </form>";

        echo $courseform;



        $editcourse = "
        <form id='updateCourseForm'>

        <p><strong>New Course Details:</strong></p>
        <table>
        <tr>
        <th>Duration (Years):</th> 
        <td id='data'>{$durationHTML}</td>
        </tr>
        </table>

        </form>
        ";

        echo $editcourse;

        ?>

        < script type = "text/javascript" > 
$(document).ready(function() {

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

    $('#coursesDrop').change(function() {

        var courseId = $(this).val(),
        coursedata;

        for (var i = 0, l = courseinfo.length; i < l; i++) {
            if (courseinfo[i].CourseId == courseId) {
                coursedata = courseinfo[i];
            }
        }

        var index = $('#newDuration option[value="' + courseinfo.Duration +  '"]').attr('selected','selected');
        alert(courseinfo.Duration);

    });
});
         < /script>

3 个答案:

答案 0 :(得分:2)

在您的代码中,您有

if (courseinfo[i].courseId = courseId) {
  coursedata = courseinfo[i];
}

你的if不好,应该是

if (courseinfo[i].courseId == courseId) {
  coursedata = courseinfo[i];
}

然后,要选择,您应该使用

$('#newDuration option[value="' + courseData.Duration +  "]').attr("selected","selected");

你需要删除$('#newDuration')[0] .selectedIndex = index;。你在for循环中的比较是使用courseId,而你的对象包含CourseId

答案 1 :(得分:2)

一个问题是:

if (courseinfo[i].courseId = courseId) {
    coursedata = courseinfo[i];
}

应该是这样的:

if (courseinfo[i].courseId == courseId) {
    coursedata = courseinfo[i];
}

您需要比较运算符==而不是=

另一个问题是以下是文字字符串而不是调用php函数:

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

查看页面来源,您会看到courseinfo的值为'<?php=json_encode($courseInfo);?>',而不是json_encode($courseInfo)的输出。你需要解决这个问题才能输出php。

修改2

您的代码中存在更多问题。以下是可行的代码。

<强>的Javascript

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

$('#coursesDrop').change(function() {

    var courseId = $(this).val(),
        coursedata, i;

    for (i = 0, l = courseinfo.length; i < l; i++) {
        if (courseinfo[i].CourseId == courseId) {
            coursedata = courseinfo[i];
        }
    }

    var index = $('#newDuration').val(coursedata.Duration);

});​

<强> Demo

以下是问题。

您需要删除courseinfo数组周围的单引号:

var courseinfo = [...];

你的if语句需要正确的变量courseinfo CourseId。在数组中CourseId所以这是if比较中需要的内容:

if (courseinfo[i].CourseId == courseId) {
    ...
}

使用coursedata.Duration设置#newDuration val

var index = $('#newDuration').val(coursedata.Duration);

答案 2 :(得分:1)

尝试:

$('#newDuration').val(courseData.Duration);

而不是

var index = $('#newDuration option[value="' + courseData.Duration +'"]').index('#newDuration option');
$('#newDuration')[0].selectedIndex = index;

---编辑---

如果courseinfo是Json字符串,您应该像这样更改代码:

var courseinfoJson = '<?php=json_encode($courseInfo);?>';
var courseinfo = $.parseJSON(courseinfoJson);

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

    for (var i = 0, i = courseinfo.length; i++) {
        if (courseinfo[i]['CourseId'] == courseId) {
            coursedata = courseinfo[i];
        }
    }

    $('#newDuration').val(courseData['Duration']);
}

---编辑---