javascript - 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
解决方案
推荐阅读
- python - DRF 不更新而是在模型序列化器中创建新实例
- python - 快速连续重复调用芹菜任务的执行时间会增加
- android - 如何使用 React Native 在 Mapview 中显示当前位置?
- java - 哈希映射
数据结构,但值是添加而不是替换 - javascript - results.length 没有为 MySQL 查询提供正确的输出?
- android - 如何从作为 AndroidEntryPoint 的 ViewPager 片段中获取父片段?
- javascript - 我可以在 Bootstrap 范围滑块中添加两个值吗
- kotlin - org.koin.core.error.InstanceCreationException:无法为 [Factory:'com.pokemonsearch.presentation.search.SearchViewModel'] 创建实例
- javascript - 对数组中的数据进行分组,求和并添加子节点
- java - 每年每个季度的第一个星期五的下星期日的 Cron 作业