【发布时间】:2017-10-18 16:54:52
【问题描述】:
很难解决这个问题。我已经搜索过了,但我不确定我用来搜索的短语是否正确。
我有一个 JSON 文件,用于从外部源引入一组数据。使用 PHP,我对内容进行解码。我创建了一个显示键的下拉框,但我现在要做的是根据选择框选择的内容,使用键附加到的不同值动态填充打印输出。
<?php
// JSON string
$json = file_get_contents("showrace.json");
$races = json_decode($json, true);
?>
<select id="raceSelect">
<option value="select one" selected>Select One</option>
<?php foreach($races as $key => $value) { ?>
<option value="<?php echo $key ?>"><?php echo $value['race'] ?></option>
<?php } ?>
</select>
<div id="template">
Str Plus: # Dex Plus: # Wis Plus: # Int Plus: #<br>
</div>
以下是 JSON 文件的示例:
{
"Ithorian":{"price":0,"wis":3,"str":2,"lck":0,"int":2,"frc":0,"dex":-2,"con":2,"cha":-3,"app":"No","hp":1200,"ac":0,"race":"Ithorian","lang":"ithorian"},
"Weequay":{"price":1000,"wis":-2,"str":3,"lck":0,"int":-2,"frc":0,"dex":0,"con":2,"cha":-3,"app":"No","hp":1350,"ac":0,"race":"Weequay","lang":"weequay"}
}
在第一个sn-p中,模板div中的#将输出JSON值,例如“str”和“dex”等。虽然我能够找到如何设置选择以从 JSON 中提取键,但我对如何根据所选项目使用相应的值填充模板部分感到困惑。
提前致谢!
【问题讨论】: