duanqin9631
duanqin9631
2017-02-06 23:45

JavaScript和PHP中的脚本 - 变量

I have a php script which shows me all folders - categories on my website:

echo "<ul>";
    foreach(glob('category/*', GLOB_ONLYDIR) as $dir) {
        $dir = str_replace('category/', '', $dir);
        echo '<li class="button" onclick="reload();">'.$dir.'</li>';
    }; 
echo "</ul>";

When I click on category JQuery reload a div with content of php file.

function reload(category) {
    $("#content").load("send.php");
}

I want to send to that php file name of the category I have had clicked. How can I do that? Thanks for the answer

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

1条回答

  • drslez4322 drslez4322 4年前

    This should work :

    echo "<ul>";
        foreach(glob('category/*', GLOB_ONLYDIR) as $dir) {
                        $dir = str_replace('category/', '', $dir);
                            echo '<li class="button" onclick="reload(\''.$dir.\'');">'.$dir.'</li>';
                    }; 
        echo "</ul>";
    

    I added the $dir variable as a parameter of the reload function.

    And then inside your reload function :

    function reload(category) {
        $("#content").load("send.php?category="+category);
    }
    

    Is this what you meant ? Hope it helped :)

    点赞 评论 复制链接分享