-1

我正在尝试使用查询选择器获取我的 .btn-viewdetail 按钮。但该函数仅获取我手动创建的按钮,而不是动态创建的按钮。我不知道为什么,它们都有相同的类名。

是不是因为,我扭曲了输入按钮?

有没有人可以解决这个问题?

window.onload=function(){
    this.getPerson();
    this.document.getElementById('getPerson').addEventListener('click', this.getPerson);
    //this.document.getElementById('deletePerson').addEventListener('click', this.deletePerson);
}

function createNode(element) {
    return document.createElement(element);
}

function append(parent, el) {
  return parent.appendChild(el);
}

const ul = document.getElementById('person');
const url ='http://localhost:8080/person';
var x = 1;



function getPerson(){
//addButton.value = "+";
//addButton.class = "btn btn-success";
$("#output1 tbody tr").remove();
fetch(url)
    .then(res => res.json())
    .then(data => {

        //console.log(data);

        var temp = "";
        data.forEach((person) => {
            temp += "<tr>";
            temp += "<td>" +person.id+"</td>";
            //console.log(person.id);
            temp += "<td>" +person.vorname+"</td>";
            temp += "<td>" +person.nachname+"</td>";
            temp += "<td>"
            person.projectList.forEach((project) => {
              temp += project.name+"<br>";
            });
            temp += "</td>"

            temp += "<td>"
            person.technoList.forEach((techno) => {
              temp += techno.name+"<br>";
            });
            temp += "</td>"

            /*id='addButton" + x + "'*/


            temp += "<td>" + addButton(person.id)+ "</td>"
             //this.parentElement.appendChild(this)
             x++;


            //document.getElementById('personDetail_Button').addEventListener('click', this.personDetail(person.id));
            temp += "</tr>"


        });

        document.getElementById("tableOutput").innerHTML= temp;

    });
}

function addButton(person_id){
  var element = document.createElement("input");
  element.type = 'button';
  element.value = 'test123';
  element.setAttribute("data-person_id",person_id);
  element.setAttribute("class","btn-viewdetail btn btn-primary");

  var _wrap = document.createElement("div");
  _wrap.appendChild(element);

 return _wrap.innerHTML;

}



document.body.innerHTML += addButton(4);


function personDetail(person_id_request){
 //console.log(person_id_request);
 fetch('http://localhost:8080/person/' + person_id_request)
    .then(res => res.json())
    .then(data => {

     console.log(data);
   });
}

function deletePerson(){
var id = document.getElementById('id_delete').value;
var id_input = document.getElementById('id_delete');
fetch('http://localhost:8080/person/' + id, {
    method: 'DELETE',
    })
    .then(res => {
        console.log(res.status)
        if(res.status == 200){
            alert("Die Person mit der ID " +id +" wurde gelöscht");
            getPerson();
            }else{
                alert("Leider konnten wir die Person nicht löschen, hast du vielleicht die falsche ID angegeben? " + res.status)

            }
        res.json();
        $('#id_delete').reset;
    })
    .then((data) => {
    })
}

var _btns = document.querySelectorAll(".btn-viewdetail");
console.log(_btns);
_btns.forEach(function (el) {
  el.addEventListener('click',function(evt){
    console.log(evt.target.getAttribute("data-person_id"));
    personDetail(evt.target.getAttribute("data-person_id"));
  });
});
4

2 回答 2

0

执行的那一刻 ,html 文档中还var _btns = document.querySelectorAll(".btn-viewdetail"); 没有元素。<input class="btn-viewdetail">

那是因为document.getElementById("tableOutput").innerHTML= temp;代码会在以后执行。

解决此问题的方法el.addEventListener('click',function(evt){是在创建输入元素之后应用单击事件侦听器var element = document.createElement("input");

它看起来像这样:

function addButton(person_id){
  var element = document.createElement("input");
  element.addEventListener('click',function(evt){
  ...
于 2020-05-31T10:54:50.817 回答
0

您必须首先通过 id 获取父元素(所有按钮的容器):

var parentEl = document.getElementById('parentEl')

接着:

var _btns = parentEl.querySelectorAll(".btn-viewdetail");

于 2020-05-31T10:58:27.437 回答