如何在下拉列表中设置focus(),在php脚本中使用javascript

时间:2012-02-22 11:16:27

标签: php javascript html focus

我无法将焦点设置到我的php页面dc-test.php(这是一个框架页面:topFrame和botFrame)下拉列表,它有2个下拉列表并且是相关的。这些下拉列表值从表中填充。无法为第一个下拉列表设置焦点,稍后在第二个从属下拉列表的重新加载后设置焦点。 Pl看下面我的代码,需要你的帮助!对不起,如果我在这个论坛上重复这个问题,因为我无法找到正确的答案。

Pl看看函数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='234!@';
$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']==@$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>

1 个答案:

答案 0 :(得分:1)

为什么不为您的选择框分配ID,然后使用

document.getElementById("cat").focus();

访问框架内的元素

window.frames[0].getElementById("cat").focus();

frames[0]只需将数字0更改为帧的索引即可。例如如果您有2个帧并且想要访问第二个帧,那么它将是frames[1]