简体   繁体   English

onclick function 只运行一次

[英]onclick function is running only once

I am using an edit button to edit the posts but when I am clicking the button the onclick function executes and it works perfectly like it edits the post and updates the post content(not from backend).我正在使用编辑按钮来编辑帖子,但是当我单击该按钮时, onclick function 会执行并且它的工作方式非常完美,就像它编辑帖子和更新帖子内容一样(不是来自后端)。 But the problem is when I click the edit button again the onclick function is not running.但问题是当我再次单击编辑按钮时 onclick function 没有运行。 My HTML code:-我的 HTML 代码:-

<div class="post">
        <b><a href="{% url 'profile' post.author.id %}">{{post.author}}</a></b>
        <input type="hidden" name="postid" value={{post.id}}>
        <!-- <br><br> -->
        <p>{{post.content}}</p>
        {% csrf_token %}
        <textarea name="edit_content" id="edit_content" cols="50" rows="5"></textarea>
        {% if user.is_authenticated %}
            {% if post.author == user %}
                <button class="edit" onclick="edit(this)">Edit</button>
            {% endif %}                
        {% endif %}
        <p><small class="text-muted">{{post.created_on}}</small></p>

Here textarea display is set to 'none'.这里文本区域显示设置为“无”。

My javascript code:-我的 javascript 代码:-

function edit(ele){
var parent = ele.parentNode;
console.log(ele.parentNode);
var post = parent.querySelector('p');
var textarea = parent.querySelector('textarea');
var id = parent.querySelector('input');

post.style.display = 'none';
textarea.style.display = 'block';
ele.innerHTML = "Save"; 
ele.id = "save";
ele.disabled = true;
const csrftoken = document.querySelector('[name=csrfmiddlewaretoken]').value;

textarea.onkeyup = () => {
    if( textarea.value.length > 0){
        ele.disabled = false;
    }
    else{
        ele.disabled = true;
    }
}

var save = document.getElementById('save');
save.onclick = () => {
    fetch("http://127.0.0.1:8000/edit", {
        method : "PUT",
        headers: {'X-CSRFToken': csrftoken},
        body : JSON.stringify({
            "postdata" : textarea.value,
            "id" : id.value
            })
    }).then(() => {
        post.innerHTML = textarea.value;
        // textarea.value = '';
        post.style.display = "block";
        textarea.style.display = "none";
        ele.innerHTML = "Edit";
        save.removeAttribute('id');
    });
}

} }

I suggest you keep the two buttons, edit and save, separated and you hide/show one of them according to the current state.我建议你保留两个按钮,编辑和保存,分开并根据当前 state 隐藏/显示其中一个。

In this demo I slightly rewrote your code so that the initial state is read only showing off the <p> with text content.在此演示中,我稍微重写了您的代码,以便仅读取初始的 state 以显示带有文本内容的<p> When you'll press the edit button, the paragraph will be hidden and the textarea with its content will show up giving you the opportunity to edit the text.当您按下编辑按钮时,该段落将被隐藏,包含其内容的文本区域将出现,让您有机会编辑文本。 At the same time the edit/save buttons will flip their visibility so that at this point when you'll press save the reverse action will be performed just after successfully calling the web api.同时,编辑/保存按钮将翻转它们的可见性,因此此时当您按下保存时,将在成功调用 web api 后立即执行反向操作。

This way you have two separated elements you can style independently and two different functions for the corresponding click events (save and edit).这样你就有了两个独立的元素,你可以独立地设置样式,并为相应的点击事件(保存和编辑)提供两个不同的功能。

As an added bonus this code doesn't deal with ids so that it could scale with multiple posts on the same page.作为一个额外的好处,此代码不处理 id,因此它可以在同一页面上处理多个帖子。 The csrf hidden field would be the only exception. csrf 隐藏字段是唯一的例外。

 function save(target){ const parent = target.parentNode; const post = parent.querySelector('p'); const textarea = parent.querySelector('textarea'); const id = parent.querySelector('input'); const edit = parent.querySelector('button.edit'); const url = "http://127.0.0.1:8000/edit"; const csrftoken = ""; fetch(url, { method: "PUT", headers: { 'X-CSRFToken': csrftoken }, body: JSON.stringify({ "postdata": textarea.value, "id": id.value }) }) //I used finally instead of then to deal with the fact that the api url will fail.finally(() => { post.innerText = textarea.value; post.style.display = "block"; textarea.style.display = "none"; target.style.display = 'none'; edit.style.display = 'block'; }); } function edit(target) { const parent = target.parentNode; const post = parent.querySelector('p'); const textarea = parent.querySelector('textarea'); const id = parent.querySelector('input'); const save = parent.querySelector('button.save'); post.style.display = 'none'; textarea.style.display = 'block'; textarea.value = post.innerText; target.style.display = 'none'; save.style.display = 'block'; }
 button.save{ display: none; cursor: pointer; } button.edit{ display: block; cursor: pointer; } textarea.edit_content{ display: none; }
 <div class="post"> <b><a href="http://bogus">{{post.author}}</a></b> <input type="hidden" name="postid" value={{post.id}}> <p>{{post.content}}</p> <textarea name="edit_content" class="edit_content" cols="50" rows="5"></textarea> <button class="edit" onclick="edit(this);">Edit</button> <button class="save" onclick="save(this);">Save</button> <p><small class="text-muted">{{post.created_on}}</small></p> <input type="hidden" name="csrfmiddlewaretoken" value="bogus"> </div>

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

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