简体   繁体   English

如何在文本输入中添加数据并在下拉菜单中选择数据?

[英]How to add data into a text input and select data in a drop down menu?

I have a simple application which yo can see here: application 我有一个简单的应用程序,您可以在这里看到: application

In the application you will see a drop down menu, simply select a course from the drop down menu and you will see the details displayed in the text inputs below: 在应用程序中,您将看到一个下拉菜单,只需从下拉菜单中选择一门课程,您将看到以下文本输入中显示的详细信息:

Below is the code for the application: 下面是该应用程序的代码:

Javascript: Javascript:

<script type="text/javascript">

$(document).ready( function(){
$('#coursesDrop').change( function(){
if( $(this).val() !== '' ){
var text = $(this).find('option:selected').text();
var split = text.split(' - ');
$('#currentCourseNo').val( split[0] );     
$('#currentCourseName').val( split[1] );       
$('#newCourseNo').val( split[0] );  
$('#newCourseName').val( split[1] ); 
}
else{
$('#currentCourseNo,#currentCourseName,#currentDuration').val('');   
$('#newCourseNo,#newCourseName,#newDuration').val('');                 
}
});
});



</script>   

PHP/HTML: PHP / HTML:

<?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;           

while ( $courseqrystmt->fetch() ) {

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


$courseHTML .= '</select>';


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

echo $courseform;



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

<p><strong>Current Course Details:</strong></p>
<table>
<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>
<div id='currentAlert'></div>

<p><strong>New Course Details:</strong></p>
<table>
<tr>
<th>Course ID:</th>
<td><input type='text' id='newCourseNo' name='CourseNoNew' value='' /> </td>
</tr>
<tr>
<th>Course Name:</th> 
<td><input type='text' id='newCourseName' name='CourseNamenew' value='' /> </td>
</tr>
<tr>
<th>Duration (Years):</th> 
<td>{$durationHTML}</td>
</tr>
</table>
<div id='newAlert'></div>

</form>

";

echo $editsession;


?>

</body>
</html>

Now I have 2 small problems which both deal with the Courses's duration: 现在,我有两个小问题,它们都与课程的持续时间有关:

Problem 1: 问题一:

In the application when you have selected the course, you can see that under the "Current Course Details" section that the Course ID and Course Name text inputs have been filled in but the Duration text input has not been filled in. Now I can't fill in the Duration text input the same way I have coded to fill in the other two text inputs because I havn't and do not want to include each course's duration in the drop down menu. 在选择课程的应用程序中,您可以看到在“当前课程详细信息”部分下,已经填写了课程ID和课程名称文本输入,但没有填写“持续时间”文本输入。现在,我可以请按照我编写其他两个文本输入所用的编码方式来填写“持续时间”文本输入,因为我没有也不想在下拉菜单中包括每门课程的持续时间。 So my question is what is the correct and best way in order to be able to fill in the Duration Text input for the selected course when the course has been selected from the drop down menu? 因此,我的问题是,当从下拉菜单中选择课程时,为了填写所选课程的“持续时间文本”输入的正确和最佳方法是什么?

Problem 2: 问题2:

This is similar to problem 1 but this deals with the Duration's Drop Down menu in the "Update Course Details" section. 这与问题1类似,但是它处理“更新课程详细信息”部分中的“持续时间”下拉菜单。 What I want is that when the user selects a course, the selected course's duration should be selected in the Duration drop down menu. 我想要的是,当用户选择一门课程时,应该在“持续时间”下拉菜单中选择所选课程的持续时间。 At the moment it still states "Please Select" for the duration drop down menu after the user has selected a course 目前,在用户选择课程后,持续时间下拉菜单仍显示“请选择”

UPDATE: 更新:

I have added id='data' to the <td>{$durationHTML}</td> . 我已将id ='data'添加到<td>{$durationHTML}</td>

Below is the javascript: 以下是javascript:

var data = document.getElementById('newDuration').value;

$('#coursesDrop').change(function()
{
  var index = this.selectedIndex;

  /* part 1 */
  var data_to_display = data[index];
  $('#data').text(data_to_display);

  /* part 2 */    
  $('#durationDrop')[0].selectedIndex = index;    

}).trigger('change');

The above code is not working as it does not select the duration from the duration drop down menu for the course selected. 上面的代码不起作用,因为它没有从所选课程的持续时间下拉菜单中选择持续时间。 What am I doing incorrectly? 我做错了什么?

The details of the course comes from the database below: 该课程的详细信息来自以下数据库:

Course Table: 课程表:

CourseId CourseNo CourseName                             Duration
1        INFO101  Information Communication Technology   3/4
2        INFO102  Computing                              2

Now my Course drop down displays the course information from the database as so: 现在,我的“课程”下拉列表显示数据库中的课程信息,如下所示:

Please Select
INFO101 - Information Communication Technology
INFO102 - Computing

Now below is what the Duration Drop Down menu looks like: 现在,下面是“持续时间”下拉菜单的外观:

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

Now if I select this course below in the course drop down menu: 现在,如果我在课程下拉菜单中选择以下课程:

INFO101 - Information Communication Technology

Then as you can see in the database the duration for this course is 3/4 , then the Duration drop down menu should select the 3/4 option once the user has selected course INFO101 - .... in the course drop down menu. 然后,如您在数据库中看到的,该课程的持续时间为3/4 ,则一旦用户在课程下拉菜单中选择了课程INFO101 - .... ,则“持续时间”下拉菜单应选择3/4选项。

Now if I select this course below in the course drop down menu: 现在,如果我在课程下拉菜单中选择以下课程:

INFO102 - Computing

Then as you can see in the database the duration for this course is 2 , then the Duration drop down menu should select the 2 option once the user has selected course INFO102 - .... in the course drop down menu. 然后,如您在数据库中看到的,该课程的持续时间为2 ,则一旦用户在课程下拉菜单中选择了课程INFO102 - .... ,则“持续时间”下拉菜单应选择2选项。

Please comment to me if you do not understand :) 如果您不明白,请给我评论:)

UPDATE 2: 更新2:

<?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=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="' + courseData.Duration + '"]').index('#newDuration option');

        $('#newDuration')[0].selectedIndex = index;

    });
});
 < /script>

Problem 1: 问题一:

You should find a way to "link" durations to items in the dropdown, so... what you don't want to do (put all in the option tag) looks to be the best answer. 您应该找到一种将持续时间“链接”到下拉列表中的项目的方法,因此...您不希望做的事情(将所有内容放入选项标签中)似乎是最好的答案。 Anyway you could write a javascript array and use the 's selectedIndex value to point to the good data, with a thing like that : 无论如何,您可以编写一个javascript数组并使用的selectedIndex值指向良好的数据,诸如此类:

$('select').change(function()
{
   var index = this.selectedIndex;
   $('input').val(duration_data[index]);
}

Problem 2: 问题2:

Same as first, as u said. 正如您所说,与第一个相同。 Use the selectIndex property to set your default value by the code. 使用selectIndex属性通过代码设置默认值。

$('select')[0].selectedIndex = n

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM