gpt4 book ai didi

javascript - Keypress event() 不会在 Firefox 中触发,而是在 Chrome 中触发

转载 作者:行者123 更新时间:2023-12-03 12:34:36 33 4
gpt4 key购买 nike

当有人按下输入文本框时,我想使用 ajax 将一些数据发布到 Controller 。我的代码在 Chrome 上运行良好,但在 Firefox 上则不行。我尝试了几种解决方案,但它对我不起作用。这是我的代码:

<input type="text"   id="txtbox_@cmt.StoryActivityId" 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="txtbox_@cmt.StoryActivityId" 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

关于javascript - Keypress event() 不会在 Firefox 中触发,而是在 Chrome 中触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23783783/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com