我有一个 JavaScript 函数,它根据类别和子类别系统刷新选择中包含的选项。这些选择是由基于 mssql 数据库内容的 php 函数生成的。
为了查询类别的层次结构,我使用 php 将此信息存储到一个二维数组中,然后使用 json_encode 将此数组传递给 JavaScript。
我的功能按预期工作,但是当用户希望更改选择中的值时,他们不能!他们只能看到可用的选项,但不能选择一个。
这是我的 JavaScript 函数:
function refresh_values() {
//the dropdown_array is my array conatining the heirarchy information
var options = "<option value=\"Tous\">Tous</option>"; //default "All" choice
var dup_array = new Array(); //to prevent doubles
if (document.getElementById('bloc').value != 'Tous') {
for (var i = 0; i <dropdown_array.length; i++) {
if (dropdown_array[i][0] == document.getElementById('bloc').value && !(dropdown_array[i][2] == null) && !(contains(dup_array, dropdown_array[i][2]))) {
dup_array.push(dropdown_array[i][2]);
options += "<option value=\"" + dropdown_array[i][2] + "\">" + dropdown_array[i][2] + "</option>";
}
}
}else {
for (var i = 0; i <dropdown_array.length; i++) {
if (!contains(dup_array, dropdown_array[i][2])) {
dup_array.push(dropdown_array[i][2]);
options += "<option value=\"" + dropdown_array[i][2] + "\">" + dropdown_array[i][2] + "</option>";
}
}
}
document.getElementById('processus').innerHTML = options;
if (document.getElementById('processus').value != 'Tous') {
for (var i = 0; i <dropdown_array.length; i++) {
if (dropdown_array[i][2] == document.getElementById('activite').value && !(dropdown_array[i][4] == null) && (!contains(dup_array, dropdown_array[i][4]))) {
dup_array.push(dropdown_array[i][4]);
options += "<option value=\"" + dropdown_array[i][4] + "\">" + dropdown_array[i][4] + "</option>";
}
}
}else {
for (var i = 0; i <dropdown_array.length; i++) {
if (!contains(dup_array, dropdown_array[i][4])) {
dup_array.push(dropdown_array[i][4]);
options += "<option value=\"" + dropdown_array[i][4] + "\">" + dropdown_array[i][4] + "</option>";
}
}
}
document.getElementById('activite').innerHTML = options;
}
这是我的“包含”功能:
function contains(a, obj) {
for (var i = 0; i < a.length; i++) {
if (a[i] === obj) {
return true;
}
}
return false;
}
这是“构建”选择的 html/php
<!--The script after each input is to preserve it's value after the form is submitted
I've already identified this as being irrelevant to my problem via testing-->
<form id="search" method="GET">
<span class="form_label">Numero: </span>
<input type="text" name="numero" value="<?php echo isset($_GET['numero']) ? $_GET['numero'] : '' ?>"/>
<br>
<span class="form_label">Bloc processus: </span>
<?php echo generate_dropdown("table_name", "nom_bloc_francais", "bloc");?>
<script type="text/javascript">
document.getElementById('bloc').value = "<?php echo $_GET['bloc'];?>";
</script>
<br>
<span class="form_label">Processus: </span>
<?php echo generate_dropdown("table_name", "nom_processus_francais", "processus");?>
<script type="text/javascript">
document.getElementById('processus').value = "<?php echo $_GET['processus'];?>";
</script>
<br>
<span class="form_label">Activite: </span>
<?php echo generate_dropdown("table_name", "nom_activite_francais", "activite");?>
<script type="text/javascript">
document.getElementById('activite').value = "<?php echo $_GET['activite'];?>";
</script>
<input type="submit" value="Search!"/>
</form>
这是最初生成选择的 php 函数:
function generate_dropdown($table, $field, $name) {
$options = "<option value=\"Tous\">Tous</option>";
$query = "SELECT DISTINCT " . $field . " FROM " . $table. " ";
$filter=mssql_query($query);
while($row = mssql_fetch_array($filter)) {
$options .= "<option value=\"". $row[$field] . "\">" . $row[$field] . "</option>";
}
$menu='
<select name="' . $name . '" class="filter" id="' . $name . '" onchange="refresh_values()">
' . $options . '
</select>';
return $menu;
}
关于用户无法从“Tous”更改选择值的原因,任何人都可以请至少指出正确的方向吗?
更多信息:当我删除 JavaScript 函数时,可以更改选择的值。
谢谢