2012-12-29 62 views
1
+---------+------------+ 
| class | name  | 
+---------+------------+ 
| 10021 | John  | 
| 10027 | Alex  | 
| 10030 | Brian  | 
| 10033 | Anita  | 
+---------+------------+ 

我想要同步我textfield,當我從ComboBox中選擇其中一個菜單:同步組合框HTML文本框與

<? 
$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的字段。

+0

你的意思是什麼?你想同時選擇Box/10021和John的值和文本在文本框中解決? –

+0

你幾乎明白我的意思,當我從comboBox中選擇「Alex」時,textfield直接顯示「10027」,因爲該字段與表格相關。 – andrian

+0

你有沒有試過下面的答案? –

回答

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='' >"; 

因爲我們正在設置文本框的值d ynamically。

2)投票:

<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/

+0

什麼是卡片類型?對不起,我從來沒有嘗試JQuery之前 – andrian

+0

我已經改變了代碼..你必須給ID屬性選擇和文本框。 –

+0

沒有概率我發佈你.. –

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>"; 
} 
?> 

如果這就是我明白你想要什麼