我想知道如何从下拉框中获取值,然后将此值粘贴到文本框中。 我正在使用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 :(得分: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>
答案 1 :(得分:0)
描述有点模糊,但我假设您正尝试这样做:http://jsfiddle.net/wp7dM/
该演示使用jQuery,我深深推荐的javascript框架。