[英]How to get the position integer of an item in php array from php to html?
I'm trying to return the position of a topic as well as the topic in a getTopics()
function. 我试图返回主题的位置以及该主题在
getTopics()
函数中的位置。 For example, returning "Musical instruments" would also return the integer 0 which can then be used in the getFaqs()
function. 例如,返回“乐器”也将返回整数0,然后可以在
getFaqs()
函数中使用该整数。 Returning "Programming languages" would also return the integer 1 and so on. 返回“编程语言”还将返回整数1,依此类推。 I don't understand why this function isn't working as I've included parameters topic_id (the integer) and topic (the text).
我不明白为什么此函数不起作用,因为我已经包含了参数topic_id(整数)和topic(文本)。
index.html: 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: topic.php:
<?php
header('Content-Type: application/json; charset=utf-8');
$topics = array("Musical instruments", "Programming languages", "Varieties of pizza");
?>
Just use a standard for
loop on the array: 只需在数组上使用标准的
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.