所以我創建<select id="VersionDropdown" onchange="change(this)"></select>
裏面的一個php文件,然後通過include
標籤拉到另一個php文件。我遇到的問題是onchange
從未被解僱。我曾嘗試使用jQuery.change()以及沒有成功。PHP select onchange not working
我使用jQuery 1.9.1
PHP:
<?php
// Build the dropdown menu based on files on the server
$dropdown = '<select id="VersionDropdown" onchange="change(this)">';
$path = './myPath/';
$blacklist = array('.', '..', 'SQL Files');
foreach (new DirectoryIterator($path) as $root) {
if($root->isDot()) continue;
$path2 = $path . $root . '/';
$MajorVersion = $root->getFilename();
$MajorVersion = str_replace('v', 'Version ', $MajorVersion);
if(fnmatch('Version 3.1', $MajorVersion)){ $MajorVersion = str_replace('Version', 'Analog Version', $MajorVersion); }
//$versions[$MajorVersion] = array();
$dropdown .= '<optgroup label="' .$MajorVersion. '">';
foreach (new DirectoryIterator($path2) as $folder) {
if($folder->isDot()) continue;
$item = $folder->getFilename();
//$versions[$MajorVersion][$item] = array();
if ($handle = opendir($path2 . $item)) {
while (false !== ($file = readdir($handle))) {
if (!in_array($file, $blacklist)) {
if(fnmatch("*.txt", $file)) {
$fileNew = str_replace('.txt', '', $file);
$versions[$MajorVersion][$item]['Version'] = $fileNew;
$dropdown .= '<option value="' .str_replace('Web', '', $item). '">' .$fileNew. '</option>';
}else{
$versions[$MajorVersion][$item][str_replace('.exe', '', $file)] = $file;
}
}
}
}
closedir($handle);
}
}
$dropdown .= '</optgroup>';
//print_r($versions);
$dropdown .= '</select>';
echo $dropdown;
?>
HTML/PHP
<html>
<title>Test Script</title>
<head>
<meta http-equiv="Content-type" content="text/html;charset=UTF-8" />
<title>Test</title>
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script>
function change(version){
alert(version);
}
$(document).ready(function() {
});
</script>
</head>
<body>
<?php include './php/version_dropdown.php'; ?>
</body>
</html>
version_download.php輸出:
<select id="VersionDropdown" onchange="change(this)">
<optgroup label="Version 5.5">
<option value="55249">5.5.24.9</option>
<option value="55225">5.5.22.5</option>
<option value="55205">5.5.20.5</option>
<option value="55183">5.5.18.3</option>
<option value="55163">5.5.16.3</option>
<option value="55141">5.5.14.1</option>
<option value="55122">5.5.12.2</option>
<option value="55106">5.5.10.6</option>
</optgroup>
<optgroup label="Version 5.0">
<option value="50016">5.0.0.16</option>
<option value="50018">5.0.0.18</option>
<option value="50022">5.0.0.22</option>
<option value="50024">5.0.0.24</option>
<option value="50284">5.0.28.4</option>
<option value="50304">5.0.30.4</option>
<option value="50321">5.0.32.1</option>
<option value="50343">5.0.34.3</option>
<option value="50403">5.0.40.3</option>
</optgroup>
<optgroup label="Version 4.3">
<option value="43062">4.3.0.62</option>
<option value="43064">4.3.0.64</option>
<option value="43066">4.3.0.66</option>
</optgroup>
<optgroup label="Version 4.2">
</optgroup>
<optgroup label="Analog Version 3.1">
</optgroup>
</select>
過去有隨機的問題是這樣,目前尚不清楚,這可能永遠工作,除非你表現出更多的碼。當選擇更改時,如何在服務器上調用php代碼? – Floris
你可以發佈一些HTML/PHP代碼嗎?我們無法猜出答案.. –
'change'函數必須在'select'元素後面定義!請添加所有代碼。 – ops