简体   繁体   English

jQuery函数不点击点击

[英]jquery function not calling on click

I have a function like so: 我有一个像这样的功能:

last_value = null;
$('td').click(function(e) {
  console.log($(e.target).attr("id"));
  if ($(e.target).is("td")) {
    var the_form = $("#edit-form").detach();
    if (last_value) {
      $("#" + last_value.attr("id")).replaceWith(last_value);
    }

    last_value = $(e.target).clone();
    $(the_form).removeClass("hidden");
    var the_input = $(the_form).find("input.form-control");
    $(the_input).attr("name", $(e.target).attr("id"));
    $(the_input).attr("placeholder", $(e.target).text());
    $(e.target).css('padding-top', 1);
    $(e.target).css('padding-bottom', 1);
    $(e.target).text("");
    $(e.target).append(the_form);
  }
});

This is supposed to take a table cell, and produce an inline form populated with the cell contents, which it replaces. 这应该采用一个表格单元格,并产生一个用单元格内容填充的内联表格,并将其替换。 Additionally, code has been added so that when a different cell is clicked, the contents revert to their original values. 此外,已添加代码,以便在单击其他单元格时,内容将恢复为其原始值。 However, the problem I'm running into is this: suppose I click one cell, A. The form appears the way it should. 但是,我遇到的问题是:假设我单击了一个单元格A。该表单以应有的方式显示。 Then suppose I click cell B. The form then "moves" to that cell, and the contents in cell A revert to their original values. 然后假设我单击单元格B。然后该窗体“移动”到该单元格,并且单元格A中的内容恢复为其原始值。 Now suppose I click on cell A again. 现在,假设我再次单击单元格A。 In this case, not only does the form not appear, it stays in cell B. In fact, the console.log doesn't even fire. 在这种情况下,不仅表单没有出现,而且还停留在单元格B中。实际上, console.log甚至不会触发。 What am I doing wrong here? 我在这里做错了什么?

Try with this 试试这个

$(document).find('td').on('click',function(e){

           e.preventDefault();
           console.log($(e.target).attr("id"));
           if ($(e.target).is("td")) {
                var the_form = $("#edit-form").detach();
                if (last_value) {
                    $("#" + last_value.attr("id")).replaceWith(last_value);
                }

                last_value = $(e.target).clone();
                $(the_form).removeClass("hidden");
                var the_input = $(the_form).find("input.form-control");
                $(the_input).attr("name", $(e.target).attr("id"));
                $(the_input).attr("placeholder", $(e.target).text());
                $(e.target).css('padding-top', 1);
                $(e.target).css('padding-bottom', 1);
                $(e.target).text("");
                $(e.target).append(the_form);
          }

    });

Hope this will help you. 希望这会帮助你。

As was suggested in the comments under the OP, (thanks @Mohammad Akbari), changing the code to 如操作规范下的注释所建议,(感谢@Mohammad Akbari),将代码更改为

$('table').on('click', 'td', function(e){
       console.log($(e.target).attr("id"));
       if ($(e.target).is("td")) {
            var the_form = $("#edit-form").detach();
            if (last_value) {
                $("#" + last_value.attr("id")).replaceWith(last_value);
            }

            last_value = $(e.target).clone();
            $(the_form).removeClass("hidden");
            var the_input = $(the_form).find("input.form-control");
            $(the_input).attr("name", $(e.target).attr("id"));
            $(the_input).attr("placeholder", $(e.target).text());
            $(e.target).css('padding-top', 1);
            $(e.target).css('padding-bottom', 1);
            $(e.target).text("");
            $(e.target).append(the_form);
      }

});

fixes the problem. 解决问题。

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

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