简体   繁体   English

单击功能不适用于第二次单击

[英]click function doesn't work for second click

I have one problem with my code. 我的代码有一个问题。 I have created this DEMO from codepen.io 我从codepen.io创建了这个DEMO

In this demo you can see there are two edit button. 在本演示中,您可以看到有两个编辑按钮。 When you click first edit button then the edit textarea will be on there. 当您单击第一个编辑按钮时,编辑文本区域将在那里。 The problem is here if you click the blue cancel button and click again first edit button it is not working. 问题出在这里,如果您单击蓝色取消按钮并再次单击第一个编辑按钮它不起作用。 What is the problem here? 这里有什么问题? Anyone can help me in this regard ? 谁能在这方面帮助我?

JS JS

$(document).ready(function() {
   $("body").on("click", ".editBtn", function(event) {
      event.target.disabled = true;
      var ID = $(this).attr("id");
      var currentMessage = $("#messageB" + ID + " .postInfo").html();
      var editMarkUp = '<div class="edBtnS"><div class="edSv">Save</div><div class="cNeD" id="'+ ID +'">Cancel</div></div><textarea rows="5" cols="80" id="txtmessage_' + ID + '">' + currentMessage + '</textarea>';
      $("#messageB" + ID + " .postInfo").html(editMarkUp);
      var data = $('#txtmessage_' + ID).val();
       $('#txtmessage_' + ID).focus();
       $('#txtmessage_' + ID).val(data + ' ');
   });
   $("body").on("click", ".cNeD", function(event) {
      event.target.disabled = false;
        var ID = $(this).attr("id");
        var currentMessageText = $("#txtmessage_" + ID).html();
        $("#messageB" + ID + " .postInfo").html(currentMessageText);
    });
});

HTML HTML

<div class="container">
   <div class="postAr" id="messageB1">
      <div class="postInfo">
         fdasfads fasd fadsf adsf adsf adsf asd fasd f dfsa
      </div>
      <div class="editBtn" name="edit" id="1">Edit1</div>
   </div>
</div>
<div class="container">
   <div class="postAr" id="messageB2">
      <div class="postInfo">
         fdasfads fasd fadsf adsf adsf adsf asd fasd f dfsassss
      </div>
      <div class="editBtn" name="edit" id="2">Edit2</div>
   </div>
</div>

Because the Edit button is disabled with : 因为“编辑”按钮被禁用:

event.target.disabled = true;

You can add in the .cNeD click event : 您可以添加.cNeD点击事件:

$(".editBtn").prop('disabled', false);

DEMO from codepen.io 来自codepen.io的演示

Edit : 编辑:

To enable only one edit button (see u_mulder comment): 要只启用一个编辑按钮(请参阅u_mulder注释):

$(this).closest('.container').find(".editBtn").prop('disabled', false);

DEMO from codepen.io 来自codepen.io的演示

In your code for handling the cancel button click, you're setting the disabled attribute to false for the cancel button . 在您办理取消按钮点击代码,你禁用属性设置为false,取消按钮 As it is, this way of doing it is kind of ugly, and I couldn't get it to work. 事实上,这种做法有点难看,我无法让它发挥作用。 I did get it to work like this: 我确实让它像这样工作:

ie,

$(document).ready(function() {
  $("body").on("click", ".editBtn", function(event) {
// changed to disable event handler          
    $(this).off('click'); 
// rest of code
  });
  $("body").on("click", ".cNeD", function(event) {
// changed to re-enable event handler for the edit button in question
    $(this).siblings('.editBtn').on('click'); 
// rest of code
  });
});

The problem is coming from event.target.disabled = true; 问题来自event.target.disabled = true;
Try removing it, it worked for me. 尝试删除它,它对我有用。

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

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