gpt4 book ai didi

javascript - 如何从php到html获取php数组中项目的位置整数?

转载 作者:行者123 更新时间:2023-12-03 05:15:39 24 4
gpt4 key购买 nike

我正在尝试返回主题的位置以及 getTopics() 函数中的主题。例如,返回“Musical Instruments”也会返回整数 0,然后可以在 getFaqs() 函数中使用该整数。返回“编程语言”也会返回整数 1 等等。我不明白为什么这个函数不起作用,因为我包含了参数 topic_id (整数)和 topic (文本)。

index.html:

function getTopics() {
$.getJSON("topics.php", function(result) {
$("#topic-container").html("");
$.each(result, function(topic_id, topic){
$("<div>", {id: topic_id , text: topic, onclick: "getFaqs(this.id, $(this).text())"}).appendTo($("#topic-container"));//pass topic_pos
});
});
return false;
}

topics.php:

<?php

header('Content-Type: application/json; charset=utf-8');
$topics = array("Musical instruments", "Programming languages", "Varieties of pizza");
?>

最佳答案

只需使用 standard for loop在数组上:

var topic, topic_id;
for (topic_id = 0; topic_id < result.length; topic_id++) {
topic = result[topic_id];
$("<div>", {id: topic_id , text: topic, onclick: "getFaqs(this.id, $(this).text())"}).appendTo($("#topic-container"));//pass topic_pos
}

关于javascript - 如何从php到html获取php数组中项目的位置整数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41618689/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com