简体   繁体   中英

PHP select onchange not working

So i am creating <select id="VersionDropdown" onchange="change(this)"></select> inside of a php file that is then pulled into another php document via include tags. The problem that i am having is that onchange is never fired off. I have tried using jQuery.change() as well with no success.

I am using 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 output:

<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>

Have you tried something like this?

 $("#VersionDropdown").change(function(){
        if($(this).val() == "something") {
              //do something
        }
    });

Try this (Edited):

$("#VersionDropdown>optgroup>option").on('click', function(event) {
    alert($(this).text());
});

When you insert a element, after the document charged, you have to use the .on() or .live() function to make it work.

More information @ jQuery

You're sending an object to the function onchange="change(this)"

Change function to:

function change(version){
    alert(version.value);
}

Or change the onchange to

onchange="change(this.value)"

jQuery not required for the above.

NOTE only one of the changes above should be made. If you make both you'll be errors

I inserted the output of your php code into the html source that you showed, giving me something that almost worked. All I then had to do was to write alert(version.value); instead of alert(version); , and I was in business. I cannot understand why that does not work for you - please confirm that this works for you, or compare with the source code that you actually get from the server:

<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.value);  // <<<<<<<< this line was changed >>>>>>>>>>>
}

$( document ).ready(function() {

}); 

</script>
</head>
<body>
<!-- <<<<<<<<<<<<<< inserted php generated code here as plain text >>>>>>>>>>>> -->
<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>
</body>
</html>

Just one other observation (which I assume to be a typo; but maybe it's the source of your problem). You are #include ing version_dropdown.php , but you call the file whose contents you are showing version_download.php . Typo? Or source of your problem... I have to believe it's a typo.

I figured it out finally. For whatever reason CHROME was not firing the onchange event!. I restarted chrome and it all of a sudden started working. LAME.

I have had random issues like this in the past where it seems like Chrome suddenly starts caching JS.. /facepalm

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM