我在一些文本输入的下方和下方有一个下拉菜单。我想要做的是,当我从下拉菜单中选择一门课程时,它应该显示在文本输入中选择的课程的相关详细信息。但是文本输入中没有显示任何内容。现在我没有收到任何错误,但我相信我在代码中做错了一些事情,因为课程详细信息没有显示在文本输入中。如何获取文本输入中显示的详细信息?
以下是下拉菜单和检索每门课程的 Duration 的 mysqli 代码:
$coursequery = "
SELECT CourseId, CourseNo, CourseName, Duration, CourseActive
FROM Course
WHERE
(CourseActive = ?)
ORDER BY CourseNo
";
... //mysqli prepare
$courseqrystmt->execute();
$courseqrystmt->bind_result($dbCourseId, $dbCourseNo, $dbCourseName, $dbCourseDuration, $dbCourseActive);
$courseqrystmt->store_result();
$coursenum = $courseqrystmt->num_rows();
$courseHTML = '';
$courseHTML = '<select name="course" id="coursesDrop">'.PHP_EOL;
$courseHTML .= '<option value="">Please Select</option>'.PHP_EOL;
$studentInfo = array();
$courseInfo = array();
while ( $courseqrystmt->fetch() ) {
$courseHTML .= sprintf("<option value='%s'>%s - %s</option>", $dbCourseId, $dbCourseNo, $dbCourseName) . PHP_EOL;
$courseData = array();
$courseData["Duration"] = $dbCourseDuration;
array_push($courseInfo, $courseData);
}
$courseHTML .= '</select>';
下面是文本输入的代码:
$editsession = "
<form id='updateForm'>
<p><strong>Course Chosen:</strong></p>
<table>
<tr>
<th></th>
<td><input type='hidden' id='currentId' name='Idcurrent' readonly='readonly' value='' /> </td>
</tr>
<tr>
<th>Course No:</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:</th>
<td><input type='text' id='currentDuration' name='Durationcurrent' readonly='readonly' value='' /> </td>
</tr>
</table>
</form>
";
echo $editsession;
下面是 jquery 代码,它根据从课程下拉菜单中选择的课程在文本输入中显示数据:
$(document).ready( function(){
var courseinfo = <?php echo json_encode($courseInfo);?>;
$('#coursesDrop').change( function(){
$('#targetdiv').hide();
var courseId = $(this).val();
if (courseId !== '') {
for (var i = 0, l = courseinfo.length; i < l; i++)
{
if (courseinfo[i].CourseId == courseId) {
var currentindex = $('#currentDuration').val(courseinfo[i].Duration);
var text = $(this).find('option:selected').text();
var split = text.split(' - ');
$('#currentId').val($(this).find('option:selected').val());
$('#currentCourseNo').val( split[0] );
$('#currentCourseName').val( split[1] );
break;
}
}
}
else{
$('#currentCourseNo,#currentCourseName,#currentDuration,#currentId,#studentselect').val('');
}
});
$('#courseForm').delegate('change','select',(function(warnings)
{
return function()
{
warnings.html('');
};
}($('#warnings'))));
});