1
+---------+------------+
| class   | name       | 
+---------+------------+
| 10021   | John       | 
| 10027   | Alex       |  
| 10030   | Brian      |  
| 10033   | Anita      |
+---------+------------+

textfield当我从组合框中选择一个菜单时,我正在尝试同步我的:

<?
$cn=mysql_connect("localhost","root") or die("Note: " . mysql_error());
$res=mysql_select_db("psi",$cn) or die("Note: " . mysql_error());
$sql = "select name, class from list;";
$res=mysql_query($sql) or die("Note: " . mysql_error());
?>

<select name="names">

<?
while($ri = mysql_fetch_array($res))
{
                //this comboBox works well
        echo "<option value=" .$ri['name'] . ">" . $ri['name'] . "</option>";
}
echo "</select> ";

echo "Class :";

        echo "<input disabled type='text' value=".$ri['class'].">". $ri['class'] . "</input>";
?>

例如,当我Alex从组合框中选择时,我textfield应该显示带有 value 的字段10027

4

2 回答 2

2

只需更改value=" .$ri['name'] to value=" .$ri['class']while 循环中的哪个,然后在更改选择框时使用 JQuery 更改文本框值...

1)jQuery

$('#idofselectbox').change(function() {
        $('#idoftextbox').val($('#cardtype :selected').val());
          /*OR    $('#idoftextbox').val($(this).val());  */
});

echo "<input disabled type='text' value=".$ri['class'].">". $ri['class'] . "</input>";
to
echo "<input disabled type='text' value='' >";

因为我们正在动态设置文本框值。

2)Javascript:

<select onChange="document.getElementById('textbox1').value=this.value">
    <option value=''>select a value</option>
    <option value='bhavin1'>bhavin1</option>
    <option value='bhavin2'>bhavin2</option>
        <option value='bhavin3'>bhavin3</option>
</select >

<input type='text' id='textbox1'>

演示:http: //jsfiddle.net/bhavinrana07/yjWmt/

于 2012-12-29T05:53:03.497 回答
1

代替

<?
while($ri = mysql_fetch_array($res))
{
    echo "<option value=" .$ri['name'] . ">" . $ri['name'] . "</option>";
}
?>

改成

<?
while($ri = mysql_fetch_array($res))
{
    echo "<option value=" .$ri['class'] . ">" . $ri['name'] . "</option>";
}
?>

如果那我明白你想要什么

于 2012-12-29T08:36:24.333 回答