0

私は以下のコードを持っています:

<a href="#" id="@item.Id" name="vote" ><img src="/Content/images/021.png" style="float:left" alt="" /></a>

ajax 呼び出しを呼び出します。最初の呼び出しで。戻り値が true の場合、2 回目の呼び出しで、投票できるのは 1 回だけであるという警告ボックスを作成します。

<script type="text/javascript">
    $(function () {
        $("div.slidera_button a").click(function (e) {
            var item = $(this);
            e.preventDefault();
            $.get('@Url.Action("VoteAjax","Home")', { id: item.attr("id") }, function (response) {
                if (response.vote == "false") {
                    alert("foo.");
                } else {
                    //something
                }
            });
        })
    });
</script>

これは機能します。ボタンをクリックしてからページを更新すると機能しますが、2 回クリックしようとすると機能しません。

ユーザーが複数回クリックできるようにしたいのですが、最初のクリックの後にのみポップアップが表示されます。

なぜこれが機能しないのですか?

どうすれば修正できますか?

編集: FF で動作します.IE では動作しません。

4

2 に答える 2

0

このスクリプトを使用する

<script type="text/javascript">
    $(function () {
        $("div.slidera_button a").live('click',function (e) {
            var item = $(this);
            e.preventDefault();
            $.get('@Url.Action("VoteAjax","Home")', { id: item.attr("id") }, function (response) {
                if (response.vote == "false") {
                    alert("foo.");
                } else {
                    //something
                }
            });
        })
    });
</script>

または、このクリックをトリガーするJQueryDelegateメソッド

于 2012-06-15T06:50:37.570 に答える
0

このようなものはどうですか:

<style type="text/css">
a.disabled {
    opacity: 0.5;
    /* whatever other styles you want */
}   
</style>

<script type="text/javascript">
    $(function () {
        $.ajaxSetup({ cache: false });
        $("div.slidera_button a").click(function (e) {
            var item = $(this);
            if (item.hasClass("disabled")) {
                // alert when they vote
                // you'll need to handle some way to add this 
                // server side to account for page reload, yes?
                alert('You may only vote once');
            }
            $.get('@Url.Action("VoteAjax", "Home")'
                , { id: item.attr("id") }
                , function (response) {
                    // if this returns successfully, you voted.
                    item.addClass('disabled');
                }
            });
            return false;
        })
    });
</script>
于 2012-06-15T06:52:42.633 に答える