首页 > 解决方案 > Get id from array of id's for a button when i click it

问题描述

i tried it using session storage but i can't this my java script code i tried

function jobHome(){
const token = "rnxdQFzTU-YuzViH78aH";
//var id = 308;

const row = document.querySelectorAll("[id=brdr]")[0];
const container = document.getElementById("conte");

const jobtitle = document.getElementById("titleforjob");
const jbtime = document.getElementById("txs3");
const jbaddress = document.getElementById("txs2");
const jbtype = document.getElementById("txs1");
const title = document.getElementById("outputtitle");
const id = document.getElementById("outputid");
const show = document.getElementById("show");

$.ajax({
    url: "https://successsroadv2.herokuapp.com/api/v1/Jobhome",
    type: "GET",
    dataType: "json",
    headers: {
      "Content-Type": "application/json",
      Authorization: token
    },
    success: function (data) {
      const myText = "";
      const addressArray = [];
      const titleArray = [];
      const typeArray = [];
      const idArray = [];
      data.map((user) => {     
        addressArray.push(user.address);
        titleArray.push(user.title);
        typeArray.push(user.jtype);
        idArray.push(user.id);
      });
      container.innerHTML = "";
      for (let i = 0; i <= 3; i++) {
        let clone = row.cloneNode(true);
        container.appendChild(clone);
        container.firstChild.innerHTML = "";
        

        jobtitle.innerHTML = data[i].title;
        jbtype.innerHTML= typeArray[i];
        jbaddress.innerHTML= addressArray[i];

        sessionStorage.setItem("jobid-"+i,idArray[i]);
        //var ites =sessionStorage.getItem("jobid-"+i);
        //console.log(ites);
        //sessionStorage.setItem('jobid', JSON.stringify(idArray));
        $('#bttn').click(function(){
          const token = "rnxdQFzTU-YuzViH78aH";
          const jbid = sessionStorage.getItem("jobid-"+i);


          const hedtitle = document.getElementById("headertitle");
          const jbtime = document.getElementById("txt3");
          const jbaddress = document.getElementById("txt2");
          const jbtype = document.getElementById("txt1");
          const desc = document.getElementById("description");
          
        
          $.ajax({
              url: 'https://successsroadv2.herokuapp.com/api/v1/Jobhome/'+jbid+'',
              type: "GET",
              dataType: "json",
              headers: {
                "Content-Type": "application/json",
                Authorization: token
              },
              success: function (data) {
                  
                 hedtitle.innerHTML=data.title; 
                 jbtype.innerHTML=data.jtype; 
                 jbaddress.innerHTML=data.address; 
                 jbtime.innerHTML=data.created_at; 
                 //desc.innerHTML=data.desc;   
                 return ;
            },
              error: function (error) {
              console.log(`Error ${error}`);
            }
          });
        });
        
      }
      
  },
  error: function (error) {
    console.log(`Error ${error}`);
  }
});

}

i need to get id of the clicked button in the div i had made an array of id the i get it from response body and make a for loop to repeat the div in HTML .. then i want to take the id for selected div when i click on the button of this div

标签: javascripthtmljquery-ajaxq

解决方案


推荐阅读