如何在下拉列表中设置焦点(),在php脚本中使用javascript
问题描述:
我无法将焦点设置到我的php页面dc-test.php(这是一个框架页面:topFrame和botFrame)下拉列表,其中有2个下拉列表和依赖。这些下拉列表值从表中填充。无法为第一个下拉列表设置焦点,以及在第二个从属下拉列表重新载入之后。在下面看到我的代码,需要你的帮助!对不起,如果我在这个论坛上重复这个问题,因为我无法找到正确的答案。如何在下拉列表中设置焦点(),在php脚本中使用javascript
查看函数firstfield(),它没有将焦点设置到下面的代码中的下拉列表中。
<html>
<head>
<SCRIPT language=JavaScript>
function reload(form)
{
var val=form.cat.options[form.cat.options.selectedIndex].value;
self.location='dc-test.php?cat=' + val ;
}
function firstfield()
{
document.topFrame.cat.focus();
}
</script>
</head>
<body onLoad="firstfield();">
<?php
$dbservertype='mysql';
$servername='localhost';
$dbusername='aaa';
$dbpassword='[email protected]';
$dbname='test';
connecttodb($servername,$dbname,$dbusername,$dbpassword);
function connecttodb($servername,$dbname,$dbuser,$dbpassword)
{
global $link;
$link=mysql_connect ("$servername","$dbuser","$dbpassword");
if(!$link){die("Could not connect to MySQL");}
mysql_select_db("$dbname",$link) or die ("could not open db".mysql_error());
}
// End of DB connection
@$cat=$_GET['cat'];
if(strlen($cat) > 0 and !is_numeric($cat)){
echo "Data Error";
exit;
}
$quer2=mysql_query("SELECT DISTINCT Ac_desc, Ac_code FROM ACMAST order by Ac_desc");
/////// Second drop down list we will check if category is selected else we will display all the subcategory/////
if(isset($cat) and strlen($cat) > 0){
$quer=mysql_query("SELECT DISTINCT Add1 FROM ACMAST where Ac_code=$cat order by Add1");
}
echo "<form name='f1'>";
echo "<table width='100%' height='39' border='0' cellpadding='2' cellspacing='0'>";
echo " <tr>";
echo " <td width='12%'>.</td>";
echo " <td width='7%'><span class='sty1'> Customer</span></td>";
echo " <td width='27%'><select name='cat' onchange=\"reload(this.form); showUser(this.value);\">
<option value=''>Select Name</option>";
while($noticia2 = mysql_fetch_array($quer2)) {
if($noticia2['Ac_code'][email protected]$cat){echo "<option selected value='$noticia2[Ac_code]'>$noticia2[Ac_desc]</option>"."<BR>";}
else{echo "<option value='$noticia2[Ac_code]'>$noticia2[Ac_desc]</option>";}
}
echo "</select>";
echo "</td>";
echo " <td width='6%'><span class='sty1'>Address</span></td>";
echo " <td width='48%'> </span><select name='subcat'>";
while($noticia = mysql_fetch_array($quer)) {
echo "<option value='$noticia[Add1]'>$noticia[Add1]</option>";
}
echo "</select>";
echo "</td>";
echo " </tr>";
echo "</table>";
?>
</body>
</html>
答
你为什么不分配一个ID,您的选择框,然后使用
document.getElementById("cat").focus();
访问框架内使用的元素
window.frames[0].getElementById("cat").focus();
frames[0]
刚将数字0更改为帧的索引。例如如果你有2帧,你想访问第二个它将是frames[1]
斯特拉顿,即使这是行不通的! – user1114409 2012-02-22 11:47:51
你有没有试过调试你的函数?使用firebug和console.log()来查看它是否得到了正确的元素 – clem 2012-02-22 12:09:25
Stratton,这对我很有用,但是问题并没有完全解决,因为当我们使用单个文件时,这是有效的:
如果我们在帧中有这个,它不会检查如何为第一帧设置焦点。 – user1114409 2012-02-22 12:46:26