简体   繁体   中英

event.target in AJAX

Hopefully, my question is not too complicated.

function showSkills(event,str) {

    xmlhttp = new XMLHttpRequest();
    xmlhttp.onreadystatechange = function() {
        if (this.readyState == 4 && this.status == 200) {
            event.target.parentElement.parentElement.parentElement.nextElementSibling.innerHTML = this.responseText;
        }
    };
    xmlhttp.open("POST","skills.php",true);
    xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
    xmlhttp.send("q=" + str);
    event.target.parentElement.parentElement.parentElement.nextElementSibling.style.display = "block";
}

The block is diplayed, so the last row of code is fine, but I cannot post $q . I know where the problem is, but I don't know how to fix it: If I put document.getElementById("skills").innerHTML = this.responseText; instead of event.target.parentElement.parentElement.parentElement.nextElementSibling.innerHTML = this.responseText; everything works fine, but only for <div id="skills'> , not for skills2 of course. I have to use this script for more div IDs (skills, skills2, skills3) separately.

My HTML:

<div class="bgimg" style="background-image:url('pic/a3.jpeg');">
  <h3 onclick="displayGrow(event)">bla bla</h3>
  <div id="sport" class="hide resetInput"><?php include 'sport.php'; ?></div>
  <div id="skills" class="hide resetInput"><?php include 'skills.php'; ?></div>
</div><!-- end of the 1st Parallax -->

<div class="bgimg" style="background-image: url('pic/a5.jpg');">
<h3 onclick="displayGrow(event)">bla bla</h3>
<div id="sport2" class="hide resetInput"><?php include 'sport.php'; ?></div>
<div id="skills2" class="hide resetInput"><?php include 'skills.php'; ?></div>
</div><!-- end of the 2nd Parallax -->

and sport.php code:

<?php
include 'cookies.php';
if ($_SERVER["REQUEST_METHOD"] == "POST") {
  if (isset($_POST['q'])) {
  $q = $_POST['q'];
}}
?>

<div class="dropdown marginTop">
<button><!-- select sport -->
<?php // "select your sport"
    if (isset($q)) {
    $result = mysqli_query($con, 'SELECT ' .$language. ' FROM sports WHERE name1="' .$q. '" LIMIT 1');
    } else {
    $result = mysqli_query($con, 'SELECT ' .$language. ' FROM page WHERE title="selSport1" LIMIT 1');
    }
    $row = mysqli_fetch_row($result);
    print_r($row[0]);
?>
</button>
<div class="dropdown-content">
<?php // sports list
     $sportlist = mysqli_query($con, 'SELECT * FROM sports WHERE title = "sports" ORDER BY ' .$language. ' ASC');
     while ($row = mysqli_fetch_array($sportlist)) {
         echo '
            <button class = "buttonList" value="' . $row[1] . '"';?>
            onclick="showSport(event, this.value); showSkills(event, this.value);" 
            <?php echo ' style="border:0;">' . $row[$language] . '</button>
        ';
     }
?>
</div>
</div>

So after showSport() was invoked, the button with $q value is diplayed in sport.php . This works fine for both div IDs: sport and sport2 aswell. Another function showSkills() should open skill.php in <div id="skills"> or in <div id="skills2"> and post $q there. The section was opened, but without $q inside. Any ideas? Would help me a lot.

event.target.parentElement.parentElement.parentElement.nextElementSibling is far too much climbing up the DOM for my taste. When I understand you right, you use the same function as a event handler for multiple elements. Why don't you just pass the ID of the particular, associated div as a parameter? I add an example below.

 // Note, I do not know what your 'str' is, so here it is just 'foo' document.getElementById('event-div-1').addEventListener('click', showSkills.bind(null, 'skills1', 'foo')); document.getElementById('event-div-2').addEventListener('click', showSkills.bind(null, 'skills2', 'foo')); document.getElementById('event-div-3').addEventListener('click', showSkills.bind(null, 'skills3', 'foo')); function showSkills(skills, event, str) { // Ajax stuff document.getElementById(skills).style.display = 'block'; } 
 .skills { display: none; } 
 <div class="skills" id="skills1">1</div> <div class="skills" id="skills2">2</div> <div class="skills" id="skills3">3</div> <div id="event-div-1">Click me for skills 1</div> <div id="event-div-2">Click me for skills 2</div> <div id="event-div-3">Click me for skills 3</div> 

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