如何使用javascript,php设置焦点在下拉列表中

时间:2012-02-21 13:08:55

标签: php javascript focus drop-down-menu set

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

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

    <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 :(得分:0)

“topFrame”完全超出范围:

function firstfield()
{
document.topFrame.cat.focus();
}

请改为:

为表单字段指定一个id:

echo "    <td width='27%'><select name='cat' id='cat' onchange=\"reload(this.form); showUser(this.value);\">

将firstfield更改为:

function firstfield()
{
document.getElementById('cat').focus();
}

并且,在一个不相关的说明:让你的生活更轻松,并开始使用jQuery。 :)