Id has changed, why can it be clicked twice?

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title></title>
</head>
<body>
    <input type="text" value="test">
    <input id="submit_button" type="button" value="submit">
    <p id="prompt">test

<script> function sendData() { var xmlHttp = new XMLHttpRequest(); xmlHttp.open("post", "write.php",true); xmlHttp.send("iq=test"); xmlHttp.onreadystatechange = function(){ if (xmlHttp.readyState == 4 && xmlHttp.status == 200){ document.getElementById("submit_button").value = "changed"; document.getElementById("submit_button").id = "changed"; document.getElementById("prompt").innerHTML = xmlHttp.responseText; } } } ob = document.getElementById("submit_button"); ob.addEventListener("click",sendData); </script> </body> </html>

the contents of write.php, just output a string

<?php
echo "no test";
?>

the above code is simple. Click

for the first time.
<input id="submit_button" type="button" value="submit">

related tags < p id= "prompt" > test

will become < p id= "prompt" > no test


at the same time

<input id="submit_button" type="button" value="submit">

becomes

<input id="changed" type="button" value="changed">

the problem is that the second time, click on this

<input id="changed" type="button" value="changed">

although there are errors, test_second.html:20 Uncaught TypeError: Cannot set property "value" of null

at XMLHttpRequest.xmlHttp.onreadystatechange (test_second.html:20)

   

ob.addEventListener ("click", sendData);

Dec.09,2021

I borrow 10 yuan from you, and then I change my name so I don't have to return it


the event is bound to the element, not to the id. Id is only used to filter out the element. Once the event is bound, it has nothing to do with the id of the element.


although the id, of the dom has been modified, the ob object always exists, and the listening event always exists the click event of logging off the dom in the
solution 1
click event

.

solution 2
use jquery's one method to listen for click events
$('- sharpsubmit_button'). One ("click", function () {});

Menu