JS:待办事项列表 – 编辑/更新项目

huangapple go评论54阅读模式
英文:

JS: TODOLIST - EDIT/Update Item

问题

I'm a beginner.. no idea how to replace new 'input value' when I click 'update' button.

I add 'input' when i click 'edit' button.

I want to write something in the new 'input' and when i click 'update(previously 'edit' button)' then want the new input value. (basically update the item or replace to the new value)

const itemName = document.querySelector('.item_name');
const editBTN = document.querySelector('.item_edit');
const newItemInput = document.createElement('input');
editBTN.addEventListener('click', (event) => {
  console.log('dfdfdfdf');
  itemName.innerHTML = '';

  newItemInput.type = 'text';
  newItemInput.className = 'newItemInput';
  itemName.appendChild(newItemInput);
  newItemInput.focus();

  editBTN.innerText = 'Update';

  updateItem();
});

function updateItem() {
  const updatedItem = newItemInput.value;
  editBTN.addEventListener('click', () => {
    const newSpan = document.createElement('span');
    itemName.appendChild(newSpan);
  })
};
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css" integrity="sha512-iecdLmaskl7CVkqkXNQ/ZH/XLlvWZOJyj7Yy7tcenmpD1ypASozpmT/E0iPtmFIB46ZmdtAc9eNBvH0H/ZpiBw==" crossorigin="anonymous" referrerpolicy="no-referrer" />

<section class="list">
  <header class="header">Shopping List</header>
  <ul class="items">
    <li class="item_row">
      <div class="item">
        <input type="checkbox" id="item_name_check" value="1" />
        <label for="item_name_check"><span class="item_name">Egg</span></label>
        <div class="button_container">
          <button class="item_edit">Edit</button>
          <button class="item_delete">
            <i class="fas fa-trash-can"></i>
          </button>
        </div>
      </div>
    </li>
  </ul>
  <form class="new_form">
    <footer class="footer">
      <input type="text" class="footer_input" />
      <button type="submit" class="footer_button">
        <i class="fas fa-plus"></i>
      </button>
    </footer>
  </form>
</section>

todolist: CRUD

Update items

Edit items

Javascript

英文:

I'm a beginner.. no idea how to replace new 'input value' when I click 'update' button.

I add 'input' when i click 'edit' button.

I want to write something in the new 'input' and when i click 'update(previously 'edit' button)' then want the new input value. (basically update the item or replace to the new value)

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

const itemName = document.querySelector(&#39;.item_name&#39;);
const editBTN = document.querySelector(&#39;.item_edit&#39;);
const newItemInput = document.createElement(&#39;input&#39;);
editBTN.addEventListener(&#39;click&#39;, (event) =&gt; {
  console.log(&#39;dfdfdfdf&#39;);
  itemName.innerHTML = &#39;&#39;;

  newItemInput.type = &#39;text&#39;;
  newItemInput.className = &#39;newItemInput&#39;;
  itemName.appendChild(newItemInput);
  newItemInput.focus();

  editBTN.innerText = &#39;Update&#39;;

  updateItem();
});

function updateItem() {
  const updatedItem = newItemInput.value;
  editBTN.addEventListener(&#39;click&#39;, () =&gt; {
    const newSpan = document.createElement(&#39;span&#39;);
    itemName.appendChild(newSpan);
  })


};

<!-- language: lang-html -->

&lt;link rel=&quot;stylesheet&quot; href=&quot;https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css&quot; integrity=&quot;sha512-iecdLmaskl7CVkqkXNQ/ZH/XLlvWZOJyj7Yy7tcenmpD1ypASozpmT/E0iPtmFIB46ZmdtAc9eNBvH0H/ZpiBw==&quot; crossorigin=&quot;anonymous&quot; referrerpolicy=&quot;no-referrer&quot; /&gt;

&lt;section class=&quot;list&quot;&gt;
  &lt;header class=&quot;header&quot;&gt;Shopping List&lt;/header&gt;
  &lt;ul class=&quot;items&quot;&gt;
    &lt;li class=&quot;item_row&quot;&gt;
      &lt;div class=&quot;item&quot;&gt;
        &lt;input type=&quot;checkbox&quot; id=&quot;item_name_check&quot; value=&quot;1&quot; /&gt;
        &lt;label for=&quot;item_name_check&quot;&gt;&lt;span class=&quot;item_name&quot;&gt;Egg&lt;/span&gt;&lt;/label&gt;
        &lt;div class=&quot;button_container&quot;&gt;
          &lt;button class=&quot;item_edit&quot;&gt;Edit&lt;/button&gt;
          &lt;button class=&quot;item_delete&quot;&gt;
            &lt;i class=&quot;fas fa-trash-can&quot;&gt;&lt;/i&gt;
          &lt;/button&gt;
        &lt;/div&gt;
      &lt;/div&gt;
    &lt;/li&gt;
  &lt;/ul&gt;
  &lt;form class=&quot;new_form&quot;&gt;
    &lt;footer class=&quot;footer&quot;&gt;
      &lt;input type=&quot;text&quot; class=&quot;footer_input&quot; /&gt;
      &lt;button type=&quot;submit&quot; class=&quot;footer_button&quot;&gt;
        &lt;i class=&quot;fas fa-plus&quot;&gt;&lt;/i&gt;
      &lt;/button&gt;
    &lt;/footer&gt;
  &lt;/form&gt;
&lt;/section&gt;

<!-- end snippet -->

todolist : CRUD

Update items

Edit items

Javascript

答案1

得分: 0

由于我们正在将 Edit 按钮重用为 Update 按钮,请在事件侦听器内部使用条件来检查它的状态,通过检查其文本内容:

editBTN.addEventListener('click', (event) => {
  if (event.target.innerText === 'Update') {
    // …
  } else {
    console.log('dfdfdfdf');
    itemName.innerHTML = '';
    // …
  }
});

我们不需要 updateItem() 函数。

在真值 if 分支中:

// 将项目名称设置为`<input>`内的值,删除`<input>`。
itemName.innerText = newItemInput.value;

// 为下一次编辑将`<input>`设置回为空。
newItemInput.value = '';

// 将Update按钮文本恢复为Edit。
editBTN.innerText = 'Edit';

将所有内容组合在一起:

const itemName = document.querySelector('.item_name');
const editBTN = document.querySelector('.item_edit');
const newItemInput = document.createElement('input');

editBTN.addEventListener('click', (event) => {
  if (event.target.innerText === 'Update') {
    itemName.innerText = newItemInput.value;
    newItemInput.value = '';
    editBTN.innerText = 'Edit';
  } else {
    console.log('dfdfdfdf');
    itemName.innerHTML = '';

    newItemInput.type = 'text';
    newItemInput.className = 'newItemInput';
    itemName.appendChild(newItemInput);
    newItemInput.focus();

    editBTN.innerText = 'Update';
  }
});
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css" integrity="sha512-iecdLmaskl7CVkqkXNQ/ZH/XLlvWZOJyj7Yy7tcenmpD1ypASozpmT/E0iPtmFIB46ZmdtAc9eNBvH0H/ZpiBw==" crossorigin="anonymous" referrerpolicy="no-referrer" />

<section class="list">
  <header class="header">Shopping List</header>
  <ul class="items">
    <li class="item_row">
      <div class="item">
        <input type="checkbox" id="item_name_check" value="1" />
        <label for="item_name_check"><span class="item_name">Egg</span></label>
        <div class="button_container">
          <button class="item_edit">Edit</button>
          <button class="item_delete">
            <i class="fas fa-trash-can"></i>
          </button>
        </div>
      </div>
    </li>
  </ul>
  <form class="new_form">
    <footer class="footer">
      <input type="text" class="footer_input" />
      <button type="submit" class="footer_button">
        <i class="fas fa-plus"></i>
      </button>
    </footer>
  </form>
</section>
英文:

Since we are reusing the Edit button as the Update button, have a conditional within the event listener to check which state it is, by checking its text content:

editBTN.addEventListener(&#39;click&#39;, (event) =&gt; {
  if (event.target.innerText === &#39;Update&#39;) {
    // …
  } else {
    console.log(&#39;dfdfdfdf&#39;);
    itemName.innerHTML = &#39;&#39;;
    // …
  }
});

We don't need the updateItem() function.

In the truthy if branch:

// Set the item name to the value inside the `&lt;input&gt;`, removing the `&lt;input&gt;`.
itemName.innerText = newItemInput.value;

// Set the `&lt;input&gt;` back to empty for the next edit.
newItemInput.value = &#39;&#39;;

// Revert the Update button text back to Edit.
editBTN.innerText = &#39;Edit&#39;;

Putting it all together:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

const itemName = document.querySelector(&#39;.item_name&#39;);
const editBTN = document.querySelector(&#39;.item_edit&#39;);
const newItemInput = document.createElement(&#39;input&#39;);

editBTN.addEventListener(&#39;click&#39;, (event) =&gt; {
  if (event.target.innerText === &#39;Update&#39;) {
    itemName.innerText = newItemInput.value;
    newItemInput.value = &#39;&#39;;
    editBTN.innerText = &#39;Edit&#39;;
  } else {
    console.log(&#39;dfdfdfdf&#39;);
    itemName.innerHTML = &#39;&#39;;

    newItemInput.type = &#39;text&#39;;
    newItemInput.className = &#39;newItemInput&#39;;
    itemName.appendChild(newItemInput);
    newItemInput.focus();

    editBTN.innerText = &#39;Update&#39;;
  }
});

<!-- language: lang-html -->

&lt;link rel=&quot;stylesheet&quot; href=&quot;https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css&quot; integrity=&quot;sha512-iecdLmaskl7CVkqkXNQ/ZH/XLlvWZOJyj7Yy7tcenmpD1ypASozpmT/E0iPtmFIB46ZmdtAc9eNBvH0H/ZpiBw==&quot; crossorigin=&quot;anonymous&quot; referrerpolicy=&quot;no-referrer&quot; /&gt;

&lt;section class=&quot;list&quot;&gt;
  &lt;header class=&quot;header&quot;&gt;Shopping List&lt;/header&gt;
  &lt;ul class=&quot;items&quot;&gt;
    &lt;li class=&quot;item_row&quot;&gt;
      &lt;div class=&quot;item&quot;&gt;
        &lt;input type=&quot;checkbox&quot; id=&quot;item_name_check&quot; value=&quot;1&quot; /&gt;
        &lt;label for=&quot;item_name_check&quot;&gt;&lt;span class=&quot;item_name&quot;&gt;Egg&lt;/span&gt;&lt;/label&gt;
        &lt;div class=&quot;button_container&quot;&gt;
          &lt;button class=&quot;item_edit&quot;&gt;Edit&lt;/button&gt;
          &lt;button class=&quot;item_delete&quot;&gt;
            &lt;i class=&quot;fas fa-trash-can&quot;&gt;&lt;/i&gt;
          &lt;/button&gt;
        &lt;/div&gt;
      &lt;/div&gt;
    &lt;/li&gt;
  &lt;/ul&gt;
  &lt;form class=&quot;new_form&quot;&gt;
    &lt;footer class=&quot;footer&quot;&gt;
      &lt;input type=&quot;text&quot; class=&quot;footer_input&quot; /&gt;
      &lt;button type=&quot;submit&quot; class=&quot;footer_button&quot;&gt;
        &lt;i class=&quot;fas fa-plus&quot;&gt;&lt;/i&gt;
      &lt;/button&gt;
    &lt;/footer&gt;
  &lt;/form&gt;
&lt;/section&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年5月14日 05:58:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/76245045.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定