我有一個基於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: <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>
嗨catalin87,謝謝你。我提出了建議,但沒有成功。還有什麼我失蹤?我用完整的代碼片段編輯了這個問題。謝謝,R – Smudger
我更新了你的代碼,讓我知道它是否工作! – catalin87
Catalin87,你是一個傳奇人物。很多人非常感謝,工作100%。真的很感謝你的時間。 – Smudger