简体   繁体   English

从编辑弹出窗口保存数据

[英]Save data from edit popup

I have a popup section with two inputs for stock values.我有一个弹出部分,其中包含两个用于股票价值的输入。 My Expected result – typed name and description should show up in the profile section after saving.我的预期结果 - 输入的名称和描述应在保存后显示在配置文件部分。

I only want to use native JS and the id is restricted.我只想使用原生 JS 并且 id 受到限制。

The issue is following - even if I type something in form inputs, input.values remain the same as defined in the HTML.问题如下 - 即使我在表单输入中输入内容,input.values 仍与 HTML 中定义的相同。

    <section class="profile">
      <div class="profile__info">
        <img class="profile__avatar" src='./images/Cousteau.png' alt='Profile Image'>
        <div class="profile__data">
          <p class="profile__name">Name</p>
          <p class="profile__description">Description</p>
        </div>
        <button class="profile__edit-button"></button>
      </div>
      <button class="profile__add-button"></button>
    </section>


    <section class="popup popup_status_is-closed">
      <div class="popup__content">
        <button class="popup__close-button"></button>
        <h3 class="popup__title">Edit</h3>
        <form class="popup__form">
          <input class="popup__input popup__input_type_name" name="name" type="text" required minlength="2" maxlength="25" value="Name">
          <input class="popup__input popup__input_type_description" name="description" type="text" required minlength="2" maxlength="35" value="Description">
          <button class="popup__button" id="submit" type="submit">Save</button>
        </form>
      </div>
    </section>

JS JS

    function formSubmitHandler(evt) {
      evt.preventDefault();

      const nameInput = popUp.querySelector('.popup__input_type_name');
      const descrInput = popUp.querySelector('.popup__input_type_description');
      const profileName = profile.querySelector('.profile__name');
      const profileDescr = profile.querySelector('.profile__description');

      nameInput.value = profileName.textContent;
      descrInput.value = profileDescr.textContent;

      closeEditForm();
    };

    saveButton.addEventListener('click', formSubmitHandler);
    formElement.addEventListener('submit', formSubmitHandler);

I think there are a number of issues with your code.我认为您的代码存在许多问题。

1) You have your assignment the wrong way around - ie nameInput.value = profileName.textContent should be profileName.textContent = nameInput.value 1)您的分配方式错误 - 即nameInput.value = profileName.textContent应该是profileName.textContent = nameInput.value

2) Your selectors are wrong - you want to select from document not from the undefined popUp , profile , etc 2)你的选择器是错误的 - 你想 select 来自document而不是来自未定义的popUpprofile

3) Your handler is wrong - you need to add the event handler to the submit button 3)您的处理程序错误 - 您需要将事件处理程序添加到提交按钮

4) I could be wrong but I think your inputs should have placeholder rather than value - that is Name and Description are simply to identify the type of input... 4)我可能是错的,但我认为你的输入应该有placeholder而不是value - 即NameDescription只是为了识别输入的类型......

With all that in mind - I made some changes and this should be working as expected考虑到所有这些 - 我做了一些更改,这应该按预期工作

 function qs(selector) { return document.querySelector(selector); } function formSubmitHandler(evt) { evt.preventDefault(); qs('.profile__name').textContent = qs('.popup__input_type_name').value; qs('.profile__description').textContent = qs('.popup__input_type_description').value; closeEditForm(); } function closeEditForm() {} document.getElementById('submit').addEventListener('click', formSubmitHandler);
 <section class="profile"> <div class="profile__info"> <img class="profile__avatar" src='./images/Cousteau.png' alt='Profile Image'> <div class="profile__data"> <p class="profile__name">Name</p> <p class="profile__description">Description</p> </div> <button class="profile__edit-button"></button> </div> <button class="profile__add-button"></button> </section> <section class="popup popup_status_is-closed"> <div class="popup__content"> <button class="popup__close-button"></button> <h3 class="popup__title">Edit</h3> <form class="popup__form"> <input class="popup__input popup__input_type_name" name="name" type="text" required minlength="2" maxlength="25" placeholder="Name"> <input class="popup__input popup__input_type_description" name="description" type="text" required minlength="2" maxlength="35" placeholder="Description"> <button class="popup__button" id="submit" type="submit">Save</button> </form> </div> </section>

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

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