简体   繁体   English

当我点击编辑按钮时出现问题,所有其他元素都听点击,但我只需要我点击的那个

[英]A problem when i click on edit button, all the other elements listen to the click but i need just the one i click on

 <.DOCTYPE html> <html lang="en"> <head> <link rel="stylesheet" type="text/css" href="css/style.css"> <meta charset="UTF-8"> <title>todo</title> </head> <body> <div class="container"> <h1 class="text">To Do App</h1> <hr> <div class="input"> <input id="input-text" type="text" name="" value=""> <button id="btn">Add</button> </div> <div class="to-do"> <h3 class="text color1">TO-do-list</h3> <hr> <ul id="toDoList"> </ul> </div> <div class="completed"> <h3 class="text color">Completed</h3> <hr> <ul id="completed"> </ul> </div> </div> <script type="text/javascript" src="js/jquery-3.4.1.min.js"></script> <script type="text/javascript" src= "js/todo.js"></script> </body> </html>

 $('#btn').on('click', function() { let newTask = $('#input-text'); if (newTask.val() === '') { alert('You need to write something'); } else { let editButton = ('<button class = edit > Edit' + '</button>'); let finishedButton = ('<button class = finished > Finished' + '</button>'); let deleteButton = ('<button class = delete > Delete' + '</button>'); let input = `<input disabled value="${newTask.val()}" >`; $('#toDoList').append('<li>' +input+ editButton + finishedButton + deleteButton + '</li>'); newTask.val(''); } $('.edit').on('click', function() { $('input').prop('disabled',false); }); $('.finished').on('click', function() { $(this).parent(); $('#completed').append($(this).parent()); }); $('.delete').on('click', function() { $(this).parent().fadeOut(500, function() { $(this).remove(); }); }); });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

A problem when i click on edit button, all the other elements listen to the click but i need just the one i click on.当我点击编辑按钮时出现问题,所有其他元素都听点击,但我只需要我点击的那个。 It's my first time working with coding, do i have another problems with my code, thank u for help in advance.这是我第一次使用编码,我的代码是否还有其他问题,提前感谢您的帮助。 A problem when i click on edit button, all the other elements listen to the click but i need just the one i click on.当我点击编辑按钮时出现问题,所有其他元素都听点击,但我只需要我点击的那个。 It's my first time working with coding, do i have another problems with my code, thx.这是我第一次使用编码,我的代码是否还有其他问题,谢谢。

Your problem is with this code:您的问题在于此代码:

  $('.edit').on('click', function() {
    $('input').prop('disabled',false);
  });

It says ALL elements with class 'edit' will make ALL 'input' elements have disabled removed.它说所有带有 class 'edit' 的元素将使所有'输入'元素都被禁用删除。

So you will have to make sure the function will only apply to the one you want to edit.因此,您必须确保 function 仅适用于您要编辑的那个。

I made a small edit here我在这里做了一个小编辑

just to show you the principal.只是为了给你看校长。 I created a counter 'count'.我创建了一个计数器“计数”。 When adding an item i up the count by 1 (count++) and asign it to a data- atttribute on the button.添加项目时,我将计数增加 1(计数++)并将其分配给按钮上的数据属性。

I also give the textfield a unique ID 'field'+count so it will be equal to the editbutton link.我还给文本字段一个唯一的 ID 'field'+count,这样它就等于编辑按钮链接。

you can see that the onlick will now use the data attribute for each edit button, so it can alter the corresponding field.您可以看到 onlick 现在将为每个编辑按钮使用 data 属性,因此它可以更改相应的字段。

There are plenty more ways to do it, but this will show you the thoughts behind the problem..有很多方法可以做到这一点,但这会告诉你问题背后的想法。

暂无
暂无

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

相关问题 当我只单击其中一个问题时,我需要帮助禁用其他问题的下拉样式 - I need HELP in disabling dropdown styles for other questions when I click on just one of them 当我点击其中一个元素时,所有元素都会消失,但只需要我没有点击的元素消失 - All the elements disappear when I click on one of them, but only need the elements that I haven't clicked on to disappear 单击按钮时,隐藏元素并显示我需要的元素 - On button click, hide elements and display the one I need 当我尝试单击一个单选按钮时,它将选择另一个 - When I try to click one radio button it selects the other 我需要在点击按钮上播放下一首歌 - I need to play next song when click on on-click button 我有一个动态 html 模板。 当我单击一个按钮时,哪个 forms 。 现在,问题是当我单击一个按钮时,所有按钮都被单击 - I have a dynamic html template. Which forms when I click on a button. Now, the problem is When I click on one button all buttons are clicked 当我第一次点击删除按钮时,所有的笔记都消失了,但是当我刷新页面时它工作得很好 - When I first click the delete button, all the notes are disappeared but, when I refresh the page it works just fine 当我单击一个时,所有可折叠项都会打开 - All Collapsibles Open When I Click One 我只是想随机点击一些按钮然后当我点击Sum按钮时它会显示总和 - I just want to randomly click some button and then when I click Sum button it will show the sum 单击折叠按钮时出现问题 - I have a problem when I click collapse button
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM