我有一个主选择标签“类别”,onchange 调用 php 脚本来“回显”新的选择标签:
<select id="category" onchange="showme(this);">
<option value="txt">text</option>
<option value="img">image</option>
<option value="htm">html</option>
<option value="aud">Audio</option>
</select>
这是调用 PHP 脚本的函数:
function showme(str) {
var e = document.getElementById("country");
var str = e.options[e.selectedIndex].value;
if (str=="") {
document.getElementById("channels").innerHTML="";
return;
}
if (window.XMLHttpRequest) {
// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
} else { // code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function() {
if (this.readyState==4 && this.status==200) {
document.getElementById("channels").innerHTML=this.responseText;
}
}
xmlhttp.open("GET","lstavai.php?q="+str,true);
xmlhttp.send();
}
这是我的 PHP 脚本:
<?php
$q = $_GET['q'];
$files = glob('../dsx/'.$q.'/*.txt');
echo '<select id="Subme" onchange="showSubme(this);">';
foreach($files as $file) {
$file = pathinfo($file);
echo '<option value="'.$file['basename'].'">'.$file['filename'].'</option>';
}
echo '</select>';
?>
因此,Subme
Select 标记由 PHP 脚本回显
,并加载到 div channels
中,一切都很好,但分配给的函数通过 PHP 脚本
onchange =“showSubme(这个);”
不起作用:-(
这个 showSubme 函数与 showme(str) (上面给出的)完全相同,并且会触发类似的 PHP 脚本。
为什么它不起作用?我在这里错过了什么吗? 我很感谢所有的答案!
最佳答案
您可能想在代码中查看一下:
var e = document.getElementById("country");
在您的 html 中,您已调用 id 类别而不是国家/地区。
<select id="category" onchange="showme(this);">
祝你编码愉快,麦克斯
关于javascript - 将函数分配给使用 PHP 脚本创建的选择,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41261643/