在ajax响应之后执行soundmanager2

时间:2014-03-05 20:16:39

标签: javascript php ajax soundmanager2

我正在尝试在ajax响应中执行脚本,似乎无法找到任何方法。我的脚本加载在html-head标签中,我也有一个id为id =“browsemusic”的div,我的ajax响应在那里。

我生成页面的php文件:

include('dbcon.php');
if(isset($_REQUEST['all']) && $_REQUEST['all'] != ''){
    //===============================Button "ALL"====================================
    unset($_REQUEST['kw']);
    unset($_REQUEST['genre']);
    $query = "select * from music";
    $result = mysqli_query($link, $query) or die (mysqli_error());
    echo '<ul id="sortable1" class="connected">';
    while($info = mysqli_fetch_array( $result )){
        echo '<li><div class="ui360"><button type="button" class="addtoplaylist" >Add</button><a href="' . $info['path'] . '"> '.$info['artist'].' - '.$info['title'].' ('.$info['album'].') '.'</a></div><hr /></li>';
    };
    echo '</ul>';
}elseif (isset($_REQUEST['kw']) && $_REQUEST['kw'] != ''){
    //============================= Search for music ================================
    $kws = $_REQUEST['kw'];
    $kws = mysqli_real_escape_string($link, $kws);

    $query = "select * from music where title like '%".$kws."%' or artist like '%".$kws."%'";
    $result = mysqli_query($link, $query) or die (mysqli_error($link));
    echo '<ul id="sortable1" class="connected">';
    while($info = mysqli_fetch_array( $result )){
        echo '<li><div class="ui360"><button type="button" class="addtoplaylist" >Add</button><a href="' . $info['path'] . '"> '.$info['artist'].' - '.$info['title'].' ('.$info['album'].') '.'</a></div><hr /></li>';
    };
    echo '</ul>';
}elseif(isset($_REQUEST['genre']) && $_REQUEST['genre'] != ''){
    //=====================================Browse By Genre ===========================================
    $genre = $_REQUEST['genre'];
    $genre = mysqli_real_escape_string($link, $genre);
    $gquery = "select music_id from musicgenre where genre_id = '$genre'";
    $results = mysqli_query($link, $gquery) or die (mysqli_error($link));
    $music=array();
    while($id_result = mysqli_fetch_array($results)){
        $music[] = $id_result['music_id'];
    };
    echo '<ul id="sortable1" class="connected">';
    foreach($music as $song){
        $query = "select * from music where music_id = '$song'";
        $result = mysqli_query($link, $query) or die (mysqli_error());;
        while($info = mysqli_fetch_array($result)){
        echo '<li><div class="ui360"><button type="button" class="addtoplaylist" >Add</button><a href="' . $info['path'] . '"> '.$info['artist'].' - '.$info['title'].' ('.$info['album'].') '.'</a></div><hr /></li>';
        };
    };
    echo '</ul>';
}else{
// ================================ Default =========================================

    $query = "select * from music";
    $result = mysqli_query($link, $query) or die (mysqli_error());
    echo '<ul id="sortable1" class="connected">';
    while($info = mysqli_fetch_array( $result )){
        echo '<li><div class="ui360"><button type="button" class="addtoplaylist" >Add</button><a href="' . $info['path'] . '"> '.$info['artist'].' - '.$info['title'].' ('.$info['album'].') '.'</a></div><hr /></li>';
    };
    echo '</ul>';

};

和我的ajax文件:

$(document).ready(function(){
    $(".all").click(function()
    {
        var all = $(this).attr("id");
        if(all != '')
        {
            $.ajax
            ({
                type: "POST",
                url: "php/searchbrowselist.php",
                data: "all="+ all,
                success: function(option)
                {
                    var $this = $("#browsemusic")
                    $this.html(option);
                    function loadjscssfile(filename, filetype){
                         if (filetype=="js"){ //if filename is a external JavaScript file
                          var fileref=document.createElement('script')
                          fileref.setAttribute("type","text/javascript")
                          fileref.setAttribute("src", filename)
                         }
                         else if (filetype=="css"){ //if filename is an external CSS file
                          var fileref=document.createElement("link")
                          fileref.setAttribute("rel", "stylesheet")
                          fileref.setAttribute("type", "text/css")
                          fileref.setAttribute("href", filename)
                         }
                         if (typeof fileref!="undefined")
                          document.getElementsByTagName("head")[0].appendChild(fileref)
                        }

                        loadjscssfile("js/soundmanager2.js", "js") //dynamically load and add this .js file
                        loadjscssfile("js/360player.js", "js") 
                    $('#sortable1, #sortable2').sortable({
                        connectWith: ".connected"
                    }).disableSelection();
                }
            });
        }
        return true;
    });
});

loadjscssfile()函数是我发现那种作品的唯一方法。问题是,如果我点击另一个调用相同文件的类型(或其他具有相同功能的类似文件的按钮),它就会停止工作。所以它只是第一次起作用。

1 个答案:

答案 0 :(得分:8)

解决了,我不得不重启()我的ajax响应中的脚本。 (soundmanager2有一个reboot()函数,所以ajax文件现在是:

$(document).ready(function(){
        $(".all").click(function()
    {
        var all = $(this).attr("id");
        if(all != '')
        {
            $.ajax
            ({
                type: "POST",
                url: "php/searchbrowselist.php",
                data: "all="+ all,
                success: function(option)
                {
                    soundManager.reboot();
                    $("#browsemusic").html(option);
                    $('#sortable1, #sortable2').sortable({
                        connectWith: ".connected"
                    }).disableSelection();
                }
            });
        }
        return false;
    });
});