2012-05-28 43 views
0

我有一個基於PHP的表單。它有一個下拉列表,從mysql數據庫中提取值。Javascript - 輸入字段的設置值等於從下拉列表中選擇的值

<select name=cat onchange="AjaxFunction(this.value);" style="width=600"> <br> 
<option value='' Select One</option> 
<br> 
<? 
    require "config.php";// connection to database 
    $q=mysql_query("select cat_id from categories"); 
    while($n=mysql_fetch_array($q)){ 
    echo "<option value=$n[cat_id]>$n[category]</option>"; 
    } 
?> 
</select> 

下拉列表工作100%。然後我還有一個文本輸入字段,

<input type=text name=copycat> 

我想山寨輸入框的值等於下拉列表中選定的值,並實時更新。

可以這樣做嗎?我應該很容易想象。我想這樣的事:

<input type=text name=copycat onBlur="document.getElementById('cat').value=this.value;"> 

任何幫助,將不勝感激。

感謝和問候, 瑞安

UPDATE

代碼來獲取javscript sendValue與模仿的價值的工作。

catalin87,請協助sendvalue工作,目前,點擊選擇按鈕沒有響應從瀏覽器。再次

感謝, 瑞安

<? 

    $con = mysql_connect('localhost', 'username', 'password'); 
if (!$con) 
    { 
    die('Could not connect to server: ' . mysql_error()); 
    } 
    $db=mysql_select_db("database", $con); 

    if (!$db) 
    { 
    die('Could not connect to DB: ' . mysql_error()); 
    } 


$sql="select packcode,category,description,grouping,packconfig,sellingunits,eottpoints from skudata order by category, packcode"; 
$result=mysql_query($sql); 

?> 

<script type="text/javascript"> 

    function AjaxFunction(cat_id) { 
    var httpxml; 
    try { 
     // Firefox, Opera 8.0+, Safari 
     httpxml = new XMLHttpRequest(); 
    } catch (e) { 
     // Internet Explorer 
     try { 
     httpxml = new ActiveXObject("Msxml2.XMLHTTP"); 
     } catch (e) { 
     try { 
      httpxml = new ActiveXObject("Microsoft.XMLHTTP"); 
     } catch (e) { 
      alert("Your browser does not support AJAX!"); 
      return false; 
     } 
     } 
    } 
    function stateck() { 
     if (httpxml.readyState == 4) { 
     var myarray = eval(httpxml.responseText); 
     // Before adding new we must remove previously loaded elements 
     for (j = document.testform.subcat.options.length - 1; j >= 0; j--) { 
      document.testform.subcat.remove(j); 
     } 
     for (i = 0; i < myarray.length; i++) { 
      var optn = document.createElement("OPTION"); 
      optn.text = myarray[i]; 
      optn.value = myarray[i]; 
      document.testform.subcat.options.add(optn); 
     } 
     } 
    } 
    var url="dd.php"; 
    url = url+"?cat_id="+cat_id; 
    url = url+"&sid="+Math.random(); 
    httpxml.onreadystatechange = stateck; 
    httpxml.open("GET",url,true); 
    httpxml.send(null); 
    } 

</script> 




<script type="text/javascript"> 
function updateinput(){ 
var e = document.getElementById("subcat"); 
var catSelected = e.options[e.selectedIndex].value; 

document.getElementById("copycat").value=catSelected; 
} 
</script> 


<script type="text/javascript"> 
function sendValue(value) 
{ 
value = e.options[e.selectedIndex].value; 
var parentId = <?php echo json_encode($_GET['id']); ?>; 
window.opener.updateValue(parentId, value); 
window.close(); 
} 
</script> 


<form name="testform"> 
Category: &nbsp; <select name=cat id=cat onchange="AjaxFunction(this.value);" style="width=600"> <br> 
<option value='' style="width=600">Select One</option> 
<br> 
<? 

    require "config.php";// connection to database 
    $q=mysql_query("select * from categories"); 
    while($n=mysql_fetch_array($q)){ 
    echo "<option value=$n[cat_id]>$n[category]</option>"; 
    } 

?> 
</select> 
<br><br> 
Pack Code: 
<select name=subcat onchange="updateinput();" > 
<br><br> 
</select> 
<br><br> 
<input type=text name=copycat id=copycat > 
<br><br> 
<td><input type=button value="Select" onClick="sendValue(document.getElementById(copycat))" /></td> 
</form> 

回答

2

1`st的ID設置爲選擇框和輸入,並添加onChange事件:

<select name=cat id=cat onchange="updateinput();" style="width=600"> 
<input type=text name=copycat id=copycat > 

然後把這個函數的地方:

<script type="text/javascript"> 
function updateinput(){ 
var e = document.getElementById("cat"); 
var catSelected = e.options[e.selectedIndex].text; 

document.getElementById("copycat").value=catSelected; 
}  
</script> 

這將填充藤條標籤所選擇的項目,如果u要選擇的項目的使用價值此功能:

<script type="text/javascript"> 
function updateinput(){ 
var e = document.getElementById("cat"); 
var catSelected = e.options[e.selectedIndex].value; 

document.getElementById("copycat").value=catSelected; 
} 
</script> 

他再是你的全碼:

 <? 

    $con = mysql_connect('localhost', 'username', 'password'); 
if (!$con) 
    { 
    die('Could not connect to server: ' . mysql_error()); 
    } 
    $db=mysql_select_db("dbname", $con); 

    if (!$db) 
    { 
    die('Could not connect to DB: ' . mysql_error()); 
    } 


$sql="select packcode,category,description,grouping,packconfig,sellingunits,eottpoints from skudata order by category, packcode"; 
$result=mysql_query($sql); 

?> 

<script type="text/javascript"> 

    function AjaxFunction(cat_id) { 
    var httpxml; 
    try { 
     // Firefox, Opera 8.0+, Safari 
     httpxml = new XMLHttpRequest(); 
    } catch (e) { 
     // Internet Explorer 
     try { 
     httpxml = new ActiveXObject("Msxml2.XMLHTTP"); 
     } catch (e) { 
     try { 
      httpxml = new ActiveXObject("Microsoft.XMLHTTP"); 
     } catch (e) { 
      alert("Your browser does not support AJAX!"); 
      return false; 
     } 
     } 
    } 
    function stateck() { 
     if (httpxml.readyState == 4) { 
     var myarray = eval(httpxml.responseText); 
     // Before adding new we must remove previously loaded elements 
     for (j = document.testform.subcat.options.length - 1; j >= 0; j--) { 
      document.testform.subcat.remove(j); 
     } 
     for (i = 0; i < myarray.length; i++) { 
      var optn = document.createElement("OPTION"); 
      optn.text = myarray[i]; 
      optn.value = myarray[i]; 
      document.testform.subcat.options.add(optn); 
     } 
     } 
    } 
    var url="dd.php"; 
    url = url+"?cat_id="+cat_id; 
    url = url+"&sid="+Math.random(); 
    httpxml.onreadystatechange = stateck; 
    httpxml.open("GET",url,true); 
    httpxml.send(null); 
    } 

</script> 


<script type="text/javascript"> 
function sendValue(value) 
{ 
    var parentId = <?php echo json_encode($_GET['id']); ?>; 
    window.opener.updateValue(parentId, value); 
    window.close(); 
} 
</script> 

<script type="text/javascript"> 
function updateinput(){ 
var e = document.getElementById("cat"); 
var catSelected = e.options[e.selectedIndex].value; 

document.getElementById("copycat").value=catSelected; 
} 
</script> 

<form name="testform"> 
Category: &nbsp; <select name=cat id=cat onchange="updateinput();" style="width=600"> <br> 
<option value='' style="width=600">Select One</option> 
<br> 
<? 

    require "config.php";// connection to database 
    $q=mysql_query("select * from categories"); 
    while($n=mysql_fetch_array($q)){ 
    echo "<option value=$n[cat_id]>$n[category]</option>"; 
    } 

?> 
</select> 
<br><br> 
Pack Code: 
<select name=subcat > 
<br><br> 
</select> 
<br><br> 
<input type=text name=copycat id=copycat > 
<br><br> 
<td><input type=button value="Select" onClick="sendValue('<?php echo $rows['packcode']; ?>')" /></td> 
</form> 

我改變:

地說:平變化= 「updateinput();」

<select name=cat id=cat onchange="updateinput();" style="width=600"> 

<input type=text name=copycat id=copycat > 

刪除:

onBlur="document.getElementById('cat').value=this.value;" 
+0

嗨catalin87,謝謝你。我提出了建議,但沒有成功。還有什麼我失蹤?我用完整的代碼片段編輯了這個問題。謝謝,R – Smudger

+1

我更新了你的代碼,讓我知道它是否工作! – catalin87

+0

Catalin87,你是一個傳奇人物。很多人非常感謝,工作100%。真的很感謝你的時間。 – Smudger

1

你應該增加id="cat"到:

<select name="cat" id="cat" onchange="AjaxFunction(this.value);" style="width=600"> 

和:

echo "<option value='".$n['cat_id']."'>".$n['category']."</option>"; 

演示:http://jsfiddle.net/V94AJ/

+0

MGraph嗨,這沒有幫助我,我已經提供了完整的代碼片段。太感謝了。 – Smudger

+0

嗨MGraph,根本沒有,模仿輸入框只是空白。 – Smudger

+1

@Smudger查看更新的答案 – mgraph

相關問題