Html选择表单 - 如何触发“动态生成的选项”
我有一个HTML表单和一个脚本,它自动触发默认的选项(选项值硬编码)。
我也有一个立场 - 独立脚本,根据MySQL中的特定列值动态创建选项选项。
问题:从下拉列表中选择时动态生成的列表,它不会触发任何东西(也不是手动,也不是自动,手动在这种情况下会很好)
自动触发的表单默认的选项:
I have a HTML form along with a script, which automatically triggers the default "option" (option values hard-coded).
I also have a stand-alone script, which creates the choices of "options" dynamically, based on a specific column values in MySQL.
Problem: When selecting from the drop-down list from the dynamically generated list, it does not "trigger" anything (nor manual, nor automatic, manual would be just fine in this case)
The form which auto-triggers the default "option":
<form>
<select name="fruit" onchange="showFruit(this.value)">
<option>Choice:</option>
<option value="1">Yellow Fruit</option>
<option value="2">Red Fruit</option>
</select>
</form>
<script>
window.onload = function () {
var el = document.getElementsByName('fruit')[0];
el.value = 1; //Set default value
el.onchange(); //trigger onchange event
}
function showFruit(val) {
alert(val);
}
</script>
生成并显示动态创建的列表的代码特定的MySQL专栏:
And the code which generates and displays the dynamically created list from a specific MySQL column:
<? $connect = mysqli_connect('localhost', '*', '*', '*');
$sql="SELECT DISTINCT(fruit_name) AS fruit_name FROM fruit ORDER BY fruit_name ASC";
$result = mysqli_query($connect, $sql);
if(mysqli_num_rows($result) > 0){
$select= '<select name="select">';
while($rs = mysqli_fetch_array($result)){
$select.='<option value="'.$rs['id'].'">'.$rs['fruit_name'].'</option>';
}
}
$select.='</select>';
echo $select;
如何将onload函数附加到动态生成选项列表?我根本不需要任何硬编码的选项。只是动态生成的一个。当我从列表中选择时,我想得到任何种类的回应/反应。
我尝试了什么:
我试图通过以下方式捕获所选选项,但无法使其工作。
How can I have the onload function to attach to the dynamically generated "options" list? I don´t need any hard-coded "options" at all. Just the dynamically generated one. And when I select from the list, I would like to get a response/reaction of any sorts.
What I have tried:
I am trying to capture the selected option via the following, but can´t get it to work.
if ($result->num_rows > 0) {
$display = true;
$select = []
while ($row = $result->fetch_assoc()) {
$select[] = "<option value=\"{$row['id']}\">{$row['fruit_name']}</option>";
}
}
connect = mysqli_connect('localhost','*', '*','*');
connect = mysqli_connect('localhost', '*', '*', '*');
sql =SELECT DISTINCT(fruit_name)AS fruit_name FROM fruit ORDER BY fruit_name ASC;
sql="SELECT DISTINCT(fruit_name) AS fruit_name FROM fruit ORDER BY fruit_name ASC";
结果= mysqli_query(
result = mysqli_query(