gpt4 book ai didi

Javascript 导致选择输入卡住

转载 作者:行者123 更新时间:2023-11-28 01:55:25 26 4
gpt4 key购买 nike

我有一个 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 函数时,可以更改选择的值。

谢谢

最佳答案

设置innerHTML 属性并不是构建Select 控件的好方法。尝试在生成innerHTML 的循环中动态构建控件。

var drpDown= document.getElementById('activite');
var newOp = document.createElement("option");
newOp.text = "Txt";
newOp.value = "1";
drpDown.options.add(newOp);

关于Javascript 导致选择输入卡住,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19190318/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com