繁体   English   中英

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

[英]How to get the position integer of an item in php array from php to html?

我试图返回主题的位置以及该主题在getTopics()函数中的位置。 例如,返回“乐器”也将返回整数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; 
}

topic.php:

<?php

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

只需在数组上使用标准的for循环即可

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
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM