繁体   English   中英

如何从依赖下拉列表获取价值到javascript并将其解析到另一个php页面?

[英]how to get value from dependant drop down list to javascript and parsing it to another php page?

我做了一个2级依赖的下拉列表,并且工作正常,但我不知道如何使用javascript ..这是我正在使用的代码。

dropdownlist.html:

<script type="text/javascript" src="../js/dropdown_list.js"></script>
<select class="level" id="level1" onChange="get_level2(this.value)">
    <option selected="selected" value="0">--Choose One--</option>

<?php
$sql_get = mysql_query ("SELECT * FROM ajax_table WHERE pid=0");

    while($row_get = mysql_fetch_array($sql_get))
    {
        echo "<option value='".$row_get['id']."'>".$row_get['category']."</option>";
    }
    ?>
    </select>

    <span id="level2"></span>

由于依赖的工作就很好..所以我想我可以跳过get_level2 ()函数和另一个php文件来处理第二级下拉菜单。

这是代码,当我单击按钮时,我尝试从下拉列表中获取值。

dropdown_list.js:

function cekForm() {
//the getValue1 work fine but I can't make getValue2 work..
//how to get value from 2nd level drop down list with getValue2??

var value1 = document.getElementById("level1");
var getValue1 = value1.options[value1.selectedIndex].value;
var value2 = document.getElementById("level2");
var getValue2 = value2.options[value2.selectedIndex].value;
if (getValue1 == 0){
alert("Please Choose One");
}
if (getValue1 != 0){
//where I want to pass the dropdown value to post_value.php
window.location= "post_value.php";
}
}

如何获取第二级下拉列表的值?以及如何将该值传递给post_value.php?

请帮我...

更新:

这是来自firefox视图页面源代码:

//this is <head> part
  <link href="../css/val.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="../js/jquery-2.0.3.min.js"></script>
<script type="text/javascript" src="../js/dropdown_list.js"></script>
//end of </head>

//<body> part
<div class="form-div">
    <form id="form_level" name="form_level" style="padding-top:0px;">
    <div class="form-row" style="padding-top:0px;">
    <h3>Choose Drop Down :</h3>
    <select class="level" id="level1" onChange="get_level2(this.value)" style="position:relative; top:-40px; left:150px;">
    <option selected="selected" value="0">--Choose One--</option>
    <option value='1'>BAA</option><option value='2'>BAK</option><option value='3'>BAUK</option></select>

    <span id="level2"></span>
    </div>


      <div class="form-row" style="position:relative; top:100px; left:305px;">
      <input class="submit" value="Send" type="button" onClick="cekForm()">
      </div>
      <br /><br />
    </form>
</div>

通过URL使用get方法传递值

<script>
function cekForm() {

    var getValue1 = document.getElementById("level1").value;
    var getValue2 = document.getElementById("level2").getElementsByTagName("select")[0].value;

    if (getValue1.length==0){
        alert("Please Choose One");
        return;
    }
    if (getValue1.length>0){
        window.location= "post_value.php?level1="+getValue1+"&level2="+getValue2;
    }
}
</script>

并在php文件中接收

post_value.php

<?php

$level1 = $_GET["level1"];
$level2 = $_GET["level2"];

?>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM