将新文本添加到待办事项列表时出现问题

为什么当我添加一个新文本时它会添加到所有元素?


$('#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 class= input>');


    $('#toDoList').append('<li>' + input + editButton + finishedButton + deleteButton + '</li>');

    $('.input').attr('value', newTask.val());

    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();

    });

  });

});

body {

  background-color: ;

}


.container {

  display: block;

  width: 400px;

  margin: 300px auto;

  box-shadow: 5px 10px 100px #888888;

  min-height: 450px;

  max-height: auto;

}


.completed {

  margin-top: 3rem;

}


.to-do {

  margin-top: 3rem;

}


#btn {

  cursor: pointer;

}


.input {

  margin-top:

}


.text {

  text-align: center;

  color: #004690;

}


.color {

  color: green;

}


.color1 {

  color: red;

}


#input-text {

  margin-left: 100px;

}


hr {

  width: 50%;

  margin-bottom: 1rem;

}

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<!DOCTYPE html>

<html>


<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">


catspeake
浏览 115回答 1
1回答

慕的地6264312

您的代码有几个问题超出了我决定忽略的问题范围。问题在于这一行,它使用类选择器来选择所有输入并更新它们的值。$('.input').attr('value', newTask.val());最简单的解决方案是使用字符串插值创建包含值的输入标记,如下所示:$('#btn').on('click', function() {&nbsp; let newTask = $('#input-text');&nbsp; if (newTask.val() === '') {&nbsp; &nbsp; alert('You need to write something');&nbsp; } else {&nbsp; &nbsp; let editButton = ('<button class = edit > Edit' + '</button>');&nbsp; &nbsp; let finishedButton = ('<button class = finished > Finished' + '</button>');&nbsp; &nbsp; let deleteButton = ('<button class = delete > Delete' + '</button>');&nbsp; &nbsp; // Use string interpolation to create input markup with value already defined&nbsp; &nbsp; let input = `<input class="input" value="${newTask.val()}">`;&nbsp; &nbsp; // Append as usual&nbsp; &nbsp; $('#toDoList').append('<li>' + input + editButton + finishedButton + deleteButton + '</li>');&nbsp; &nbsp; // Below line is no longer needed so is commented out&nbsp; &nbsp; //$('.input').attr('value', newTask.val());&nbsp; &nbsp; // Your code continues unmodified&nbsp; &nbsp; newTask.val('');&nbsp; }&nbsp;&nbsp;&nbsp; $('.edit').on('click', function() {&nbsp; &nbsp; $('.input').prop('disabled', false);&nbsp; });&nbsp;&nbsp;&nbsp; $('.finished').on('click', function() {&nbsp; &nbsp; $(this).parent();&nbsp; &nbsp; $('#completed').append($(this).parent());&nbsp; });&nbsp; $('.delete').on('click', function() {&nbsp; &nbsp; $(this).parent().fadeOut(500, function() {&nbsp; &nbsp; &nbsp; $(this).remove();&nbsp; &nbsp; });&nbsp; });});body {&nbsp; background-color: ;}.container {&nbsp; display: block;&nbsp; width: 400px;&nbsp; margin: 300px auto;&nbsp; box-shadow: 5px 10px 100px #888888;&nbsp; min-height: 450px;&nbsp; max-height: auto;}.completed {&nbsp; margin-top: 3rem;}.to-do {&nbsp; margin-top: 3rem;}#btn {&nbsp; cursor: pointer;}.input {&nbsp; margin-top:}.text {&nbsp; text-align: center;&nbsp; color: #004690;}.color {&nbsp; color: green;}.color1 {&nbsp; color: red;}#input-text {&nbsp; margin-left: 100px;}hr {&nbsp; width: 50%;&nbsp; margin-bottom: 1rem;}<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script><!DOCTYPE html><html><head>&nbsp; <link rel="stylesheet" type="text/css" href="css/style.css">&nbsp; <meta charset="UTF-8">&nbsp; <title>todo</title></head><body>&nbsp; <div class="container">&nbsp; &nbsp; <h1 class="text">To Do App</h1>&nbsp; &nbsp; <hr>&nbsp; &nbsp; <div class="input">&nbsp; &nbsp; &nbsp; <input id="input-text" type="text" name="" value="">&nbsp; &nbsp; &nbsp; <button id="btn">Add</button>&nbsp; &nbsp; </div>&nbsp; &nbsp; <div class="to-do">&nbsp; &nbsp; &nbsp; <h3 class="text color1">TO-do-list</h3>&nbsp; &nbsp; &nbsp; <hr>&nbsp; &nbsp; &nbsp; <ul id="toDoList">&nbsp; &nbsp; &nbsp; </ul>&nbsp; &nbsp; </div>&nbsp; &nbsp; <div class="completed">&nbsp; &nbsp; &nbsp; <h3 class="text color">Completed</h3>&nbsp; &nbsp; &nbsp; <hr>&nbsp; &nbsp; &nbsp; <ul id="completed">&nbsp; &nbsp; &nbsp; </ul>&nbsp; &nbsp; </div>&nbsp; </div>&nbsp; <script type="text/javascript" src="js/jquery-3.4.1.min.js"></script>&nbsp; <script type="text/javascript" src="js/todo.js"></script></body></html>
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript