douyu7210 2013-05-26 11:18
浏览 62
已采纳

无法从选择框的onchange事件向javascript函数发送2个值

I am trying to write a code where in I am dynamically generating select box options. I need to send 2 values from this onchange event. 1 is the sub_header_id and other is the header_id. I need to send $row["header_id"] to the javascript function. Following is my PHP code. How do I do that? I don't want to send a concatenated string and then split it in javascript function.

<?php
    include('config.php');
    $sql = sprintf("SELECT * FROM retail_sub_headers");
    $result = mysql_query($sql);
    $num = mysql_num_rows($result);
    if ($num > 0)
    {
        echo "<form>";
        echo "<select id = \"my_select_box\" onChange = \"javascript:load_brand_option_box(this, 'populate_brands', '');\">";
        echo "<option selected value = \"-------\">-------</option>";
        while($row = mysql_fetch_array($result))
        {
            echo "<option value = ".$row["sub_header_id"].">".$row["sub_header_name"]."</option>";
        }
        echo "</select>";   
        echo "</form>";
    }
?>

And here is my javascript function javascript:load_brand_option_box.

function load_brand_option_box (sub_header_id, action, brand_id)
{
    var sub_header_value = sub_header_id.options[sub_header_id.selectedIndex].value
     if (action == "populate_brands")
     {
        if (window.XMLHttpRequest)
        {  // code for IE7+, Firefox, Chrome, Opera, Safari
            xmlhttp=new XMLHttpRequest();
        }
        else
        {// code for IE6, IE5
            xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
        }   
        xmlhttp.onreadystatechange=function()
        {
            if (xmlhttp.readyState==4 && xmlhttp.status==200)
            {
                document.getElementById("brand_names").innerHTML=xmlhttp.responseText;
                //document.getElementById("livesearch").style.border="1px solid #A5ACB2";
            }
        }   
        xmlhttp.open("GET","populate.php?action="+action+"&sub_header_id="+sub_header_value+"&brand_id="+"",true);
        xmlhttp.send();
    }
}
  • 写回答

1条回答 默认 最新

  • doushichi3678 2013-05-26 12:14
    关注

    Try this.... Added a newval attribute in option for header id you can now grab header id inside the function

    <?php
            include('config.php');
            $sql = sprintf("SELECT * FROM retail_sub_headers");
            $result = mysql_query($sql);
            $num = mysql_num_rows($result);
            if ($num > 0)
            {
                echo "<form>";
                echo "<select id = \"my_select_box\" onChange = \"load_brand_option_box(this, 'populate_brands', '');\">";
                echo "<option selected value = \"-------\">-------</option>";
                while($row = mysql_fetch_array($result))
                {
                    echo "<option value = ".$row["sub_header_id"]."  newval=".$row["header_id"].">".$row["sub_header_name"]."</option>";
                }
                echo "</select>";   
                echo "</form>";
            }
        ?>
    
        function load_brand_option_box (sub_header_id, action, brand_id)
        {
            var sub_header_value = sub_header_id.options[sub_header_id.selectedIndex].value
            var sel = document.getElementById('my_select_box');
            var header_id=sel.options[sel.selectedIndex].getAttribute('newval');
            //you will get the header id here 
             if (action == "populate_brands")
             {
                if (window.XMLHttpRequest)
                {  // code for IE7+, Firefox, Chrome, Opera, Safari
                    xmlhttp=new XMLHttpRequest();
                }
                else
                {// code for IE6, IE5
                    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
                }   
                xmlhttp.onreadystatechange=function()
                {
                    if (xmlhttp.readyState==4 && xmlhttp.status==200)
                    {
                        document.getElementById("brand_names").innerHTML=xmlhttp.responseText;
                        //document.getElementById("livesearch").style.border="1px solid #A5ACB2";
                    }
                }   
                xmlhttp.open("GET","populate.php?action="+action+"&sub_header_id="+sub_header_value+"&brand_id="+"",true);
                xmlhttp.send();
            }
        }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 用visual studi code完成html页面
  • ¥15 聚类分析或者python进行数据分析
  • ¥15 逻辑谓词和消解原理的运用
  • ¥15 三菱伺服电机按启动按钮有使能但不动作
  • ¥15 js,页面2返回页面1时定位进入的设备
  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?