简体   繁体   中英

html file button not clicking

i have this code that checks if the user has logged in or not, if the user has not logged in, it redirects the user to the login page to log in and if he/she has logged in, they would be allowed to upload content on the page. this is the code:

let vocals = document.getElementsByClassName("up");// class name of certain divs in the program
for (let i = 0; i < vocals.length; i++) {
  vocals[i].addEventListener("click", () => {
    let check = "php/checkCookie.php";
    fetch(check, { method: "GET" })
      .then((res) => res.text())
      .then((data) => {
        if (data == 0) {
          shout("Login Before You Can Create", 0);
          setInterval(() => {
            location.href = "logincheck.html";
          }, 3000);
        } else {
          document.getElementById(`e${i}`).click();
        }
      });
      ...
  });
}

the code:

document.getElementById(`e${i}`).click();

is supposed to open the file explorer so that the user can pick a file and upload, but it doesn't work, and it does not show an error.

those anyone know why and has a solution, thanks in advance

Maybe your element doesn't support click() on it? If I remember correctly, click() works on on several elements, but not all. It should work on <input> , and <a> .

You could also try this instead: document.getElementById(`e${i}`).dispatchEvent(new MouseEvent('mousedown')) document.getElementById(`e${i}`).dispatchEvent(new MouseEvent('mouseup'))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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