下面我有一个下拉菜单:
$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('');
}
});
});