jQuery Focusout 输入不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19265571/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 23:33:30  来源:igfitidea点击:

jQuery Focusout of input not working

javascriptjquery

提问by MarkHowes

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:

我已经尝试了以下所有内容,都在 $(document).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");
    });

回答by Rohan Kumar

Try this,

尝试这个,

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

If parenttd.editnot works then use documentas parent selectorlike

如果parenttd.edit不起作用,则documentparent selector这样使用

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

Fiddle

小提琴

回答by Amit

Try this

尝试这个

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

回答by Mahak Choudhary

What is your jquery version because focusout function added in the jquery1.4 Here is ref. http://api.jquery.com/focusout/

你的jquery版本是什么,因为在jquery1.4中添加了focusout函数这里是ref。http://api.jquery.com/focusout/

回答by Wojtek Mazurek

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 focusoutevent actualy doesn't occur. Think about it. You make new input, You click away and it seems that it should trigger the event. But it doesn't because inputwasn't focused in the first place.

问题是该focusout事件实际上并没有发生。想想看。你创建 new input,你点击离开,它似乎应该触发事件。但这不是因为一开始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).

我在代码中包含的另一个升级是从正在编辑的其他表格单元格中删除输入,以便一次只能编辑一个(因为人们忘记了然后“繁荣”发生了)。