0

下面我有一个下拉菜单:

 $courseHTML = '';

.... //sql code to perform query goes here

    $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>';

下面是 html 表格,它将在表格中显示课程的当前详细信息:

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

    <p><strong>Current Course Details:</strong></p>
    <table>
    <tr>
    <th></th>
    <td><input type='hidden' id='currentCourseId' name='CourseIdcurrent' value='' /> </td>
    </tr>
    <tr>
    <th>Course ID:</th>
    <td><input type='text' id='currentCourseNo' name='CourseNocurrent' readonly='readonly' value='' /> </td>
    </tr>
    <tr>
    <th>Course Name:</th>
    <td><input type='text' id='currentCourseName' name='CourseNamecurrent' readonly='readonly' value='' /> </td>
    </tr>
    <tr>
    <th>Duration (Years):</th>
    <td><input type='text' id='currentDuration' name='Durationcurrent' readonly='readonly' value=''/> </td>
    </tr>
    </table>    
    </form>

";

我在下面有一个 jquery 代码,它根据从下拉菜单中选择的选项在上面的文本输入中显示详细信息。它用细节填充文本输入没有问题。但我的问题是,假设发生的是,如果用户在下拉菜单中选择“请选择”选项,那么所有文本输入都应该是空的。问题是它仍然在文本输入中显示信息,尽管我在“请选择”选项上。

如果从下拉菜单中选择了“请选择”选项,我需要在下面的代码中更改什么以便能够清除文本输入?

$(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 currentindex = $('#currentDuration').val(coursedata.Duration);
        var editindex = $('#newDuration').val(coursedata.Duration);
        var currentid = $('#currentCourseId').val(coursedata.CourseId);
        var editid = $('#newCourseId').val(coursedata.CourseId);

            if( $(this).val() !== '' ){
                var text = $(this).find('option:selected').text();
                var split = text.split(' - ');
                $('#currentCourseNo').val( split[0] );     
                $('#currentCourseName').val( split[1] );       
            }
            else{
                $('#currentCourseNo,#currentCourseName,#currentDuration,#currentCourseId').val('');           
            }
        });
        });
4

1 回答 1

1

问题:

有一个错误,当我再次选择“PLease”选择选项时,错误控制台中显示错误,错误表明 coursedata 在这一行未定义var currentindex = $('#currentDuration').val(coursedata.Duration);

解释:

当您选择“请选择”时 courseId 为空白,因此在 for 循环中永远不会满足 if 条件,因此永远不会设置 coursedata。

解决方案:

首先检查一个有效的(不是“请选择...”)选择,然后进行工作。我还重新组织了 JS 的其他一些部分,所以它更有意义。

$(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('');
        }
    });
});
于 2012-12-06T06:57:22.533 回答