简体   繁体   English

PHP选择onchange不起作用

[英]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. 因此,我在php文件中创建<select id="VersionDropdown" onchange="change(this)"></select> ,然后通过include标记将其拉入另一个php文档。 The problem that i am having is that onchange is never fired off. 我遇到的问题是onchange永远不会触发。 I have tried using jQuery.change() as well with no success. 我也尝试使用jQuery.change(),但没有成功。

I am using jQuery 1.9.1 我正在使用jQuery 1.9.1

PHP: 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 / 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: 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>

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. 当您插入元素时,在文档收费后,您必须使用.on().live()函数使其起作用。

More information @ jQuery 更多信息@ jQuery

You're sending an object to the function onchange="change(this)" 您正在将对象发送到函数onchange="change(this)"

Change function to: 将功能更改为:

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

Or change the onchange to 将onchange更改为

onchange="change(this.value)"

jQuery not required for the above. 上面不需要jQuery。

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. 我将您的php代码的输出插入到您显示的html源中,这给了我几乎可行的东西。 All I then had to do was to write alert(version.value); 然后,我要做的就是编写alert(version.value); instead of alert(version); 而不是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 . 您正在#include version_dropdown.php ,但是您调用了显示了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!. 无论出于何种原因,CHROME均未触发onchange事件!。 I restarted chrome and it all of a sudden started working. 我重新启动了chrome,然后突然开始工作了。 LAME. 瘸。

I have had random issues like this in the past where it seems like Chrome suddenly starts caching JS.. /facepalm 过去,我曾经遇到过类似的随机问题,好像Chrome突然开始缓存JS .. / facepalm

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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