简体   繁体   English

jQuery Focusout输入无法正常工作

[英]jQuery Focusout of input not working

I'm trying to create in-line editing of data in a table, to do this I am changing the text in the cell into an input. 我正在尝试在表中创建数据的内联编辑,为此我将单元格中的文本更改为输入。

$(document).ready(function(){
    $('td.edit').on("click",function(){
        $(this).html("<input type=\"text\" value=\"" + $.trim($(this).text()) + "\" />");
        $(this).off();
    });
});

This works fine. 这很好用。

Then I'm wanting to write the data away with ajax when I click away, however I can't seem to get the focusout working... 然后,当我点击时,我想用ajax写出数据,但是我似乎无法让焦点工作......

I've tried the following, all inside the $(document).ready, all without success: 我已经尝试了以下内容,所有这些都在$(文档).ready中,都没有成功:

    $('td.edit').on("focusout","input",function(){
        alert("bye");
    });

    $('td.edit input').on("focusout",function(){
        alert("bye");
    });

    $('td.edit input').focusout(function(){
        alert("bye");
    });

    $('td.edit').on("focusout",function(){
        alert("bye");
    });

    $('td.edit').focusout(function(){
        alert("bye");
    });

Try this, 试试这个,

$('td.edit').on("blur","input",function(){
    alert("finally bye");
});

If parent td.edit not works then use document as parent selector like 如果parent td.edit不起作用,那么使用document作为parent selector

$(document).on("focusout","td.edit input",function(){
    alert("finally bye");
});

Fiddle 小提琴

Try this 试试这个

 $('td.edit').on("blur",function(){
    alert("bye");
});

What is your jquery version because focusout function added in the jquery1.4 Here is ref. 什么是你的jquery版本,因为在jquery1.4中添加了focusout函数这里是ref。 http://api.jquery.com/focusout/ http://api.jquery.com/focusout/

I know it may be too late now but I've just had exactly the same problem and I solved it but first I will show my code and then talk about it. 我知道现在可能为时已晚,但我刚刚遇到了完全相同的问题,我解决了它,但首先我会展示我的代码,然后再谈谈它。

    $('.price').click(function() {
      //If TD has no input (#in) element
      if(!$(this).find('#in').length) {
        //If there is any input element among children of all TD elements
        if($('#in').length) {
          //Set the input's parent's text as a value of input
          $('#in').parent().text($('#in').val());
          //Do Your AJAX using value from above
        }
        //Dynamicaly add input element
        $(this).html('<input type="number" value="'+$(this).text()+'" id="in">');
        //Set focus on input element
        $('#in').focus();
      }
    });
    $('.price').focusout(function() {
      $('#in').parent().text($('#in').val());
      //Do Your AJAX using value from above
    });

The problem is that the focusout event actualy doesn't occur. 问题是focusout事件实际上不会发生。 Think about it. 想一想。 You make new input , You click away and it seems that it should trigger the event. 你创建了新的input ,你点击了它似乎应该触发事件。 But it doesn't because input wasn't focused in the first place. 但事实并非如此,因为input并非首先集中在一起。

Another upgrade I included in my code is deleting input from other table cells that are being edited so that only one at the time can be edited (because people forget and then "boom" happens). 我在代码中包含的另一个升级是删除正在编辑的其他表格单元格的输入,以便当时只能编辑一个(因为人们会忘记然后“繁荣”发生)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM