0

JavaScript や jQuery に詳しくありませんが、「お気に入り」ページでブログ投稿を追加および削除し、保存された投稿のカウンターを更新する関数を作成する必要があります。これを行うことができるプラグインまたはスニペットの準備ができているソリューションはありますか?

これが私のHTMLスニペットです。

<h1>
    <a href="http://www.example.com/add-post-to-my-favorites-page.htm" id="post_0064">
        <span class="bookmark" title="My Favorites — Add/Remove">Favorites </span>Heading
    </a>
</h1>
<p>Body copy.</p>
[...]
<ul class="ul_favs">
[...]
    <li id="bookmarks">
        <a href="http://www.example.com/account/favs.htm">My Favorites</a>
        <sup><!-- Counter -->46</sup>
    </li>
</ul>

でできると思いますが$.ajax、方法がわかりません。たぶん、このようなものですか?

4

2 に答える 2

1

jQuery won't actually delete anything. If you want to really remove items, you'll have to do so at the source of the list. If your list is made up of static-HTML, you'll need a language like PHP that can access the raw-file and make changes. If your list is stored in a database, you'll need a server-side language like PHP or C# to make those changes.

jQuery can post data to server-side scripts that have the ability to remove/add/edit entries in a database. You might have a PHP script like the following:

if ($_POST) {
  $favid = $_POST["favid"];
  remove_favorite($favid);
}

jQuery could pass a favid to this script:

$.post("removefav.php", {favid:121});

This would post a variable to the server-side script, which would then take the value of that posted-variable and delete its corresponding record in the database.

This is a very crude example, but should suffice for getting you a bit more understanding of jQuery's relationship to server-side languages and databases.

于 2009-08-18T15:29:24.760 に答える
0
function favorites() {
    $(".bookmark").click(function(event) {
        var request = $(this).parent.attr("id");
        var counter = parseInt($("#bookmarks sup").text());
        if ($(this).hasClass("ed")) {
            //Remove bookmark
            $.post("/account/favorites/remove.htm", { favid:request },
                    function() {
                        $("#bookmarks sup").text(--counter); // Decrease counter
                        $(this).toggleClass("ed"); //Toggle class of clicked element
                    });
        } else {
            //Add bookmark
            $.post("/account/favorites/add.htm", { favid:request },
                    function() {
                        $("#bookmarks sup").text(++counter); // Increase counter
                        $(this).toggleClass("ed"); //Toggle class of clicked element
                    });
        }
    });
}
于 2009-08-18T18:09:41.223 に答える