2011-05-29 62 views
0

我想知道如何从下拉框中获取值,然后将此值粘贴到文本框中。 我正在使用HTML和PHP。如何从下拉框中获取数值?


这是我的代码。我做了Lucanos做的,但没有奏效。

<html> 
    <body> 
    <title>Num One Website</title> 
    <? $con = mysql_connect("localhost", "username", "password"); 
     if (!$con) { 
     die('Could not connect: ' . mysql_error()); 
     } 
     mysql_select_db("dbname", $con); 
     $result1 = mysql_query("SELECT * FROM Students") ; 

     echo "<FORM>"; 
     echo "<select id='userid' name='userid' onchange='this.form.elements['showuserid'].value=this.options[this.selectedIndex].value'>"; 

     while($row1 = mysql_fetch_array($result1)) { 
     echo "<option value='".$row1['ID']."'>".$row1['ID']."</option>"; 
     $count = $count + 1; 
     } 
     echo "</select>"; 
     echo "</FORM>"; 

     mysql_close($con); 
    ?> 

    <form action="ConfirmEnter.php" method="post"> 
     <p> 
     <input id="showuserid" name="showuserid" readonly="readonly" size=5/> 
     <input type="text" name="name" /> 
     <input type="text" name="mt" size=5 value="0.0"/> 
     <input type="text" name="pr" size=5 value="0.0" /> 
     <input type="text" name="fi" size=5 value="0.0" /> 
     <input type="text" name="tot" size=5 /> 
     </p><br><br> 
     <input type="submit" value="Submit" /><br> 
    </form> 

    </body> 
</html> 
+0

心态表现出一定的代码? – 2011-05-29 12:00:24

+0

Lucanos代码不适合你,因为你将下拉标签放在单独的表单标签中。它应该与showiserid字段的形式相同。 – IvanGL 2011-06-17 05:36:10

回答

2

凭借简单的JavaScript(无库):

<form> 
<select id="userid" name="userid" onchange="this.form.elements['showuserid'].value=this.options[this.selectedIndex].value"> 
    <option value="">Select a User</option> 
    <option value="1">First User</option> 
    <option value="9">Ninth User</option> 
</select><br> 
<input id="showuserid" name="showuserid" readonly="readonly"> 
</form> 

使用jQuery:

<form> 
<select id="userid" name="userid"> 
    <option value="">Select a User</option> 
    <option value="1">First User</option> 
    <option value="9">Ninth User</option> 
</select><br> 
<input id="showuserid" name="showuserid" readonly="readonly"> 
</form> 
<script type="text/javascript"> 
$(document).ready(function(){ 
    $('#userid').change(function(){ 
    $('#showuserid').val($(this).val()); 
    }); 
}); 
</script>