javascript(post-in-post)

javascript(post-in-post),javascript,asp.net,Javascript,Asp.net,我的代码: <script type="text/javascript"> window.onload = function () { $.post("/Game/GetPlayers", function (data) { for (var i = 0; i < data.player.length; i++) { if

我的代码:

    <script type="text/javascript">
            window.onload = function () {
                  $.post("/Game/GetPlayers", function (data) {
                    for (var i = 0; i < data.player.length; i++) {
                        if ("@Session.SessionID" == data.player[i].AgainstId);
                        {
                           //some code
                        }
                        $("<li></li>").text(data.player[i].Name).on("click", function () {
\*when i click - field hide */            $(this).hide();
                      $.post("/Game/SetId", { name: data.player[i].Name },function(data2) {
                                alert(data2);
                            });
                        }).prependTo("ol");
                    }
                });
            }
        </script>

window.onload=函数(){
$.post(“/Game/GetPlayers”,函数(数据){
对于(var i=0;i”).text(data.player[i].Name)。在(“单击”上,函数(){
\*单击-field hide*/$(this.hide())时;
$.post(“/Game/SetId”,{name:data.player[i].name},函数(data2){
警报(数据2);
});
}).prependTo(“ol”);
}
});
}

为什么当我点击时标签(li)消失了,但第二个post请求不起作用?是否可能(请求中的请求)?

我认为
li
点击导致数据错误
。播放器[I]未定义

这是因为使用了闭包变量
i
。然后单击事件发生
i
将具有值
data.player.length
data.player[data.player.length]
未定义

$(function() {
    $.post("/Game/GetPlayers", function(data) {
        $.each(data.player, function(i, v) {
            if ("@Session.SessionID" == v.AgainstId) {
                // some code
            }
            $("<li></li>").text(v.Name).on("click", function() {
                // when i click - field hide
                // $(this).hide();
                $.post("/Game/SetId", {
                            name : v.Name
                        }, function(data2) {
                            alert(data2);
                        });
            }).prependTo("ol");
        });
    });
})
$(函数(){
$.post(“/Game/GetPlayers”,函数(数据){
$.each(data.player,function(i,v){
如果(“@Session.SessionID”==v.AgainstId){
//一些代码
}
$(“
  • ”)。文本(v.Name)。在(“单击”,函数()上){ //当我单击时-字段隐藏 //$(this.hide(); $.post(“/Game/SetId”{ 姓名:v.姓名 },函数(数据2){ 警报(数据2); }); }).prependTo(“ol”); }); }); })
    另一个选项是使用事件委派

    $(function() {
        $.post("/Game/GetPlayers", function(data) {
            $.each(data.player, function(i, v) {
                if ("@Session.SessionID" == v.AgainstId) {
                    // some code
                }
                $("<li></li>").text(v.Name).data("player",
                        v.name).prependTo("ol");
            });
        });
    
        $('ol').on('click', 'li', function() {
            var $this = $(this);
            // when i click - field hide
            // $(this).hide();
            $.post("/Game/SetId", {
                name : $this.data('player')
            }, function(data2) {
                alert(data2);
            });
        });
    });
    
    $(函数(){
    $.post(“/Game/GetPlayers”,函数(数据){
    $.each(data.player,function(i,v){
    如果(“@Session.SessionID”==v.AgainstId){
    //一些代码
    }
    $(“
  • ”).text(v.Name).data(“播放器”, v、 名称)。预本托(“ol”); }); }); $('ol')。在('click','li',function()上{ var$this=$(this); //当我单击时-字段隐藏 //$(this.hide(); $.post(“/Game/SetId”{ 名称:$this.data('player')) },函数(数据2){ 警报(数据2); }); }); });
    有一个
    语句末尾,如果
    语句,请将其删除