将combobox html与textfield同步

时间:2012-12-29 05:47:05

标签: php combobox sync textfield

+---------+------------+
| 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应该会显示值为10027的字段。

2 个答案:

答案 0 :(得分:2)

只需更改while循环中的value=" .$ri['name'] to value=" .$ri['class'], 然后用于更改选择框 使用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/

答案 1 :(得分: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>";
}
?>

如果那就是我明白你想要的东西