【发布时间】:2015-05-29 09:07:25
【问题描述】:
我想根据用户在第一个下拉框中选择的值来填充我的第二个下拉框。这是我到目前为止所做的:
在 PHP 文件中:
function displayDropDown()
{
$table_tester = "TBL_TESTER_LIST";
$query_string = "select * from $table_tester";
$result = @mysql_query($query_string) or die (mysql_error());
echo "<select id=\"tester_type\" onChange=\"getTesterType();\">";
while($data = mysql_fetch_array($result))
{
$tester_type = $data['tester_type'];
echo "<option value='$tester_type'>$tester_type</option>"; // first drop down
}
echo "</select>";
}
function displaySecondDropDown($selected_tester_type)
{
$table_tester = "TBL_TESTER_LIST";
$query_string = "select * from $table_tester where tester_type = '$selected_tester_type'";
$result = @mysql_query($query_string) or die (mysql_error());
echo "<select>";
while($data = mysql_fetch_array($result))
{
$tester_name = $data['tester_name'];
echo "<option value='$tester_name'>$tester_name</option>";// second drop down
}
echo "</select>";
}
?>
<?php
$action = rtrim($_REQUEST['action']);
if($action=="insert")
{
$tester_type = rtrim($_REQUEST['tester_type']);
$tester_name = rtrim($_REQUEST['tester_name']);
echo checkDuplicateTesterName($tester_type, $tester_name);
}
else if($action=="displayDropDown")
{
$selected_tester_type = $_REQUEST['tester_type'];
echo displayDropDown();
echo displaySecondDropDown($selected_tester_type);
}
?>
在外部 JavaScript 文件中:
function displayDropDown()
{
var page = "database.php";
$.post(page, {
action : "displayDropDown"
}, function(data) {
$("div#drop_two_list").html(data);
});
}
function getTesterType()
{
var tester_type = $("#tester_type").val();
var page = "database.php";
$.post(page, {
tester_type : tester_type
}, function(data) {
$("div#drop_two_list").html(data);
});
}
在 HTML 文件中:
<html>
<head>
<title>Delete Tester</title>
<link rel="stylesheet" type="text/css" href="style.css"/>
<script language="javascript" type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="function.js"></script>
<script type="text/javascript">displayDropDown();</script>
</head>
<body>
<h3>Delete Tester</h3>
<div id="drop_two_list"></div>
<table class="deleteTable">
<tr>
<td/><td><br>
<input type="button" value="Cancel" onclick="window.close();"/>
<input type="button" name="send" value="Delete" onclick="deleteTester();"/>
</td>
</tr>
</table>
</body>
</html>
确实会出现两个下拉框。第一个有值(这是错误的,我将在后面解释),第二个是空的。此外,出现错误:Notice: Undefined index: tester_type in /opt/lampp/htdocs/Signup/module1/database.php on line 86,即 PHP 文件中的$selected_tester_type = $_REQUEST['tester_type'];。如果我要在第一个值中选择一个值,那么另一个错误会将页面上的所有内容替换为:Notice: Undefined index: action in /opt/lampp/htdocs/Signup/module1/database.php on line 75,即$action = rtrim($_REQUEST['action']);。
现在到我说明为什么第一个下拉框具有 错误 值的部分。在继续之前,为了更好地理解我的问题,这里是我试图填充下拉框的表(TBL_TESTER_LIST)。 注意:我只以随机顺序显示 5 个示例行,因为我在此表中有超过 500 行,并且不想将所有内容都放在这里。
id tester_type tester_name
1 LMX LMX-01
2 LMX LMX-04
26 LCX LCX-06
40 CAT CAT-14
95 HPPS HPPS-01
tester_type 列是我想填充我的第一个下拉框的内容,而 tester_name 列是我想相应地填充我的第二个下拉框的内容。如您所见,tester_type 有重复项,因为一个 tester_name 可以属于同一个 tester_type(例如,green apple no.1(tester_name) 是 apple(tester_type),green apple no.2(tester_name) 也是苹果(tester_type))。
我目前在第一个下拉框中得到的只是:LMX、LMX、LMX、LMX 等等。那是因为我的前 31 行是 LMX。如何对我的第一个下拉框进行编码,使其仅显示每种类型中的一种,而我的第二个下拉框将显示基于用户选择的值?
例如:如果用户在第一个下拉框中选择 LMX,则所有属于 LMX tester_type 的 tester_name 将填充第二个下拉框。
如果需要有关表格的更多信息,请告诉我。一直在寻找解决方案 5 天,我似乎还没有接近结论。
【问题讨论】:
-
在两个 JS 函数中都需要传递
action和tester_type。对于displayDropDown函数,action的值为“displayDropDown”,tester_type的值为“”。在第二个功能。将action也设置为“displayDropDown”。希望对您有所帮助。 -
嗯,这可能是错误之一,但我仍然遇到同样的问题。
标签: javascript php jquery html mysql