重新载入下拉框内容?

时间:2016-09-01 13:46:47

标签: javascript php html

我一直都没有,也没有什么对我有用所以我不得不问社区他们是否有任何东西给我。

所以,我有一个下拉菜单和一个开关(复选框),如果我按下开关它应该加载下拉内容,让我们说" Radio"如果我将其切换回来,它应该将下拉内容切换回让我们说"播放列表"。

以下是代码:

<?PHP
// Read playlist & Radio
    $playlists =    explode("\n", shell_exec("/usr/bin/mpc lsplaylists"));
    array_pop($playlists); //remove empty last line

    $radioStations  = array();
    $radioStations =    explode("\n", shell_exec("/usr/bin/mpc ls Webradio"));
    $radioStations = str_replace("Webradio/","",$radioStations);
    $radioStations = str_replace(".pls","",$radioStations);
    array_pop($radioStations); //remove empty last line
?>

脚本:

<script>

        function checkSelectedScheduleType()
        {
            if (document.getElementById('radioSelected').checked) 
            {
                alert("radio selected fill dropdown with radio stations...");

                <?php 
                $playlist = $radioStations;
                ?>
            } 
            else 
            {
                <?php 
                $playlist = $playlists;
                ?>
                alert("Playlist selected fill dropdown with playlists...");
            }
        }


</script>

开关:

<td class="alarmvalue" style="padding:2px 15px;">
        <select name="add[alarmplaylist]" id="playlist_wrongstyle" class="form-control"  style="visibility:visible; background: transparent; width:250px;">
            <?php foreach ($playlist as $playlistss){echo "<option value=\"$playlistss\">$playlistss</option>\n";} ?>
        </select>
</td>

1 个答案:

答案 0 :(得分:0)

我发现的问题:

即使PHP是在JS&#34; IF&#34;声明,php仍然在没有调用JS函数的情况下运行,这很奇怪,所以select选择了最后一个php变量。

我的修复:

可能不是最好但是有效!我创建了另一个下拉列表,填充了我想要的选项,然后使Display =&#34; none&#34;然后当我点击它将触发我的功能&#34; onchange&#34;并使其显示=&#34;阻止&#34;互相隐藏,工作正常!

以下代码:

JS:

<script>
        function checkSelectedScheduleType()
        {
            if (document.getElementById('radioSelected').checked) 
            {
                document.getElementById("slplaylist").style.display="none";
                document.getElementById("slradio").style.display="block";
            } 
            else 
            {
                document.getElementById("slplaylist").style.display="block";
                document.getElementById("slradio").style.display="none";
            }
        }
</script>

HTML:

<td class="alarmvalue" style="padding:2px 15px;">
        <label class="switch-lightp2 well_" onchange="checkSelectedScheduleType()">
            <input id="radioSelected" name="" type="checkbox" value="">
            <span><span>Playlist</span><span>Radio</span></span><a class="btn btn-primary"></a>
        </label>
    </td>

    <td class="alarmcat">

    </tr>

    <td class="alarmvalue" id="slplaylist" style="padding:2px 15px; display:block;">
        <select name="add[alarmplaylist]" id="playlist_wrongstyle" class="form-control"  style="visibility:visible; background: transparent; width:250px;">
            <?php foreach ($playlists as $playlist){echo "<option value=\"$playlist\">$playlist</option>\n";} ?>
        </select>
    </td>

    <td class="alarmvalue" id="slradio" style="padding:2px 15px; display:none;">
        <select name="add[alarmplaylist]" id="playlist_wrongstyle" class="form-control"  style="visibility:visible; background: transparent; width:250px;">
            <?php foreach ($radioStations as $playlist){echo "<option value=\"$playlist\">$playlist</option>\n";} ?>
        </select>
    </td>