当有人按下输入文本框时,我想使用ajax将一些数据发布到控制器。我的代码在Chrome上工作正常,但在Firefox中却无法正常工作。我尝试了几种解决方案,但对我不起作用。这是我的代码:

<input type="text"   id="[email protected]" onkeypress="Reply(this)"/>




<script>

function Reply(e){
    var id;
    id = e.id;


        var keycode = (event.keyCode ? event.keyCode : event.which);
        if (event.keyCode == '13') {
            //var txt1 = "\"";
            //var txt2 = txt1.concat(id);
            //var txt3 = "\"";
            //var ActivityId = txt2.concat(txt3);
            var storyActivityId = id.replace("txtbox_", "");
            var liId = '#' + "liPartial_" + storyActivityId;
            var txtId='#'+id;
            //event.stopPropagation();

            $.ajax({

                url: '@Url.Action("PostReply", "Feed")',
               type: 'post',
               cache: false,
               async: true,
               InsertionMode: 'InsertionMode.InsertAfter',
               data: { id:e.id,status:$(txtId).val()},
               success: function (data) {
                   $(liId).append("<br>" + data);
                   $(txtId).val('');
               }
           })
    }
}

最佳答案

您需要将事件传递给该函数,

<input type="text"   id="[email protected]" onkeypress="Reply(event)"/>


脚本

function Reply(event) {
    var id;
    id = this.id;
    var keycode = (event.keyCode ? event.keyCode : event.which);
    alert(keycode);
    if (event.keyCode == '13') {
        //var txt1 = "\"";
        //var txt2 = txt1.concat(id);
        //var txt3 = "\"";
        //var ActivityId = txt2.concat(txt3);
        var storyActivityId = id.replace("txtbox_", "");
        var liId = '#' + "liPartial_" + storyActivityId;
        var txtId = '#' + id;
        //event.stopPropagation();

        $.ajax({

            url: '@Url.Action("PostReply", "Feed")',
            type: 'post',
            cache: false,
            async: true,
            InsertionMode: 'InsertionMode.InsertAfter',
            data: {
                id: e.id,
                status: $(txtId).val()
            },
            success: function (data) {
                $(liId).append("<br>" + data);
                $(txtId).val('');
            }
        })
    }
}


Demo

09-20 02:23