JQuery 模糊和回车键 - 模糊不起作用

JQuery blur and enter key - blur not working

本文关键字:模糊 不起作用 回车 JQuery      更新时间:2023-09-26

我正在使用以下内容进行内联编辑。

我希望 ajax 在按下输入键或用户点击离开时触发(模糊)

当按下回车键时,一切正常,但 AJAX 没有在模糊时触发?

// inline editing
$(document).on('blur keypress', 'span[contenteditable=true]', function (e) {
    if (e.type == 'blur' || e.keyCode == '13') {
        $.ajax({
           type:'POST',
           url:'/ajax/actions/editGenre.php',
           data:{
             content: $(this).text(),
             id: $(this).attr('rowId')
           },
           success:function(msg){
             $('span[contenteditable=true]').removeClass('inlineEdit').addClass('editAble');
           }
         });
         e.preventDefault(); 
         }
});

修复:JSFiddleWiddleBiddleBoDiddle(注释掉了你的AJAX小提琴,并使用警报来演示)

$(document).on('focusout keypress', 'span.inlineEdit', function (e) {
    e.preventDefault();
    if (e.type == 'focusout' || e.keyCode == '13') {
        $.ajax({
            type: 'POST',
            url: '/ajax/actions/editGenre.php',
            data: {
                content: $(this).text(),
                id: $(this).attr('rowId')
            },
            success: function (msg) {
                $('span.inlineEdit').removeClass('inlineEdit').addClass('editAble');
            }
        });
    }
});

模糊事件不会针对 span 标记触发。 使用其他旨在接收焦点的东西。