无法从文本框中的下拉列表中获取名称

时间:2014-11-04 00:15:23

标签: javascript php html

我有一个页面,我动态生成文本框和下拉列表。我试图从下拉列表中选择一个名称,然后在旁边的文本框中显示它。问题是我能够只为第一行做到这一点我的意思是我能够从第一个下拉列表中选择一个名称,然后在它旁边的文本框中显示它。但我无法为其他人这样做。你能帮忙吗?

这是我的代码

请注意:我使用JavaScript来显示用户从DropDown列表中选择的内容并在其中显示              文本框

<?php
    if(isset($_POST['team'])){
         $selectedTeam = $_POST['team'];
         $selectedWeek = $_POST['week'];

        //getting team member names from resolver table 
        $query = "SELECT * FROM `names` WHERE `teamName` = '$selectedTeam'";
        $query_run_teamName = mysql_query($query);
         while($query_row = mysql_fetch_assoc($query_run_teamName)){
            $verNAME[]  = $query_row['verNAME'];
        }//end of while
    }//end of if condition
?>
<!DOCTYPE html>
<html>
  <head>
    <title>Option Page</title>
    <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"></script>
    <script type="text/javascript" src="roster.js"></script>
    <link rel="stylesheet" type="text/css" href="style.css">
    <script type="text/javascript">

        //Function below gets ID and assigns value to the left text field
        function getValue(RightTextfield, leftTextfield){
                var TextfieldValue     = _(RightTextfield).value;   
                _(leftTextfield).value = TextfieldValue;
        }//end of getValue() function 

        function _(x){
             return document.getElementById(x);
        }//end of _(x) function 
    </script>
  </head>
  <body>
     <div id="wrapper">
        <form action="teamRoster.php" enctype="multipart/form-data" method="POST">
            <div id="response"></div><!--End of response Div-->
            <?php

               for ($i = 0; $i <= $count; $i++) {
                    echo '<div class="STeam">';

                    //code below to generate Text Fields dynamically 
                    echo '<input type="text" id="r1Value1" name="r1Value1">';

                        //code below to generate dropdown list dynamically 
                        echo '<select id="ChooseMember" name="ChooseMember" onchange="getValue(\'ChooseMember\', \'r1Value1\')">';      
                        echo '<option value="">Select Team Member</option>';
                                foreach ($verNAME as $key) {
                                    echo '<option value="'.@$key.'">'.@$key.'</option>';
                        }
                        echo '</select>';
                    echo '</div>';
                }//End of for loop
             ?>
            <!--Passing week and team name as hidden values-->
            <input type="hidden" id="HiddenDay" name="HiddenDay" value="<?php echo $selectedWeek;?>">
            <input type="hidden" id="HiddenTeam" name="HiddenTeam" value="<?php echo $selectedTeam;?>">
        </form>
  </body>
</html>

谢谢

1 个答案:

答案 0 :(得分:0)

嗯,我认为您应该检查您的选择框ID和名称。您在选择框中输入相同的ID和名称。我的建议是使用选择框ID和名称附加$ i的值(特别是使用javascript的ID,但是由于来自PHP的抓取值,你应该追加到名称中)

For select box HTML -
<select id="ChooseMember_<?=$i?>" name="ChooseMember_<?=$i?>" onchange="getValue(\'ChooseMember_<?=$i?>\', \'r1Value1\')">

此问题是为相同的ID和选择框名称创建。