首页 > 解决方案 > Can I use jQuery to have users add/remove content to a site?

问题描述

I want to be able to have users edit a list on a website and have the data they enter stay there after refresh. I have this code to add and remove data -

HTML -

        <div class="list-editor">
            <ul class="my-list">
              <li><span>My to-do</span> <a href="#" class="delete">[done]</a></li>
            </ul>
            <input type="text" name="new-item" /> <button class="button">Add an item</button>
          </div>

jQuery -

    $(".list-editor").on("click", "button", function(){
        let val = $("input[type='text']").val()
        $(".my-list").append(`<li>${val}  <a href="#" class="delete">[done]</li>`)
        $("input[type='text']").val('Enter Text')
    })

    $(".list-editor").on('click','.delete',function() {
        $(this).parent().css("text-decoration", "line-through")
      })

What can I add to my jQuery code to get the data entered to remain on the site after refresh?

标签: jqueryappend

解决方案


推荐阅读