下面我有一个下拉菜单:
$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代码,它在上面的文本输入中显示详细信息,具体取决于从下拉菜单中选择的选项。它填充文本输入与细节没有问题。但我的问题是,假设发生的是,如果用户在下拉菜单中选择“请选择”选项,则所有文本输入都应为空。问题是它仍然在文本输入中显示信息,尽管我在“PLease Select”选项上。
如果从下拉菜单中选择“请选择”选项,我需要在下面的代码中更改以便能够删除文本输入?
$(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('');
}
});
});
答案 0 :(得分:1)
问题:
出现错误,我在错误控制台中显示错误 再次选择“PLease”选择选项,错误说明 coursedata在此行
上未定义var currentindex = $('#currentDuration').val(coursedata.Duration);
说明:
当您选择“请选择”以及if条件时,courseId为空白 永远不会在for循环中遇到,因此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('');
}
});
});