gpt4 book ai didi

php - JQuery:从数据库加载数据并将其插入以选择

转载 作者:行者123 更新时间:2023-11-29 09:14:25 25 4
gpt4 key购买 nike

我有三个选择,如下所示:

<select name="nick">
<?php foreach ($chars as $char):?>
<option value='<?php echo $char['name'] ?>'><?php echo $char['name'] ?></option>
<?php endforeach; ?>
</select>
<select name='faction' disabled>
<option value='ally'>Alliance</option>
<option value='horde'>Horde</option>
</select>
<select name='class' id="class" disabled>
<option value='1'>Warrior</option>
<option value='2'>Paladin</option>
<option value='3'>Hunter</option>
<option value='4'>Rogue</option>
<option value='5'>Priest</option>
<option value='6'>Death Knight</option>
<option value='7'>Shaman</option>
<option value='8'>Mage</option>
<option value='9'>Warlock</option>
<option value='10'>Druid</option>
</select>

$字符:

$chars = array(0 => array("name" => "Solock",
"level" => "80",
"class" => "9"),
1 => array("name" => "Emmorts",
"level" => "80",
"class" => "3"),
2 => array("name" => "Ghom",
"level" => "80",
"class" => "2"),
);
$json = json_encode($chars);

JSON 数组:

[
{"name":"Solock","level":"80","class":"9"},
{"name":"Emmorts","level":"80","class":"3"},
{"name":"Ghom","level":"80","class":"2"}
]

JSON 对象:

{
"0":{"name":"Solock","level":"80","class":"9"},
"1":{"name":"Emmorts","level":"80","class":"3"},
"2":{"name":"Ghom","level":"80","class":"2"}
}

是否有可能,每次我更改 [name=nick] 选项时,其他选择都会选择 JSON 对象或数组中的数据(如果我选择“Ghom”,它会选择联盟和死亡骑士)?怎么办?

谢谢。

最佳答案

首先 - 在服务器上并根据数据库条目 - 创建一个 JSON 字符串:

{
"Solock": ["ally", "9"],
"Emmorts": ["ally", "3"],
"Ghom": ["ally", "6"]
}

接下来,在客户端上将该 JSON 字符串加载到 JavaScript 对象中。现在,您可以使用 jQuery 和该对象:

var $selectNick = $('select[name=nick]'),
$selectFaction = $('select[name=faction]'),
$selectClass = $('select[name=class]');

$selectNick.change(function() {
var value = this.value;
$selectFaction.val( json[value][0] );
$selectClass.val( json[value][1] );
});

(此处,json 变量是从 JSON 字符串创建的对象。)

工作演示: http://jsfiddle.net/tgeVh/

关于php - JQuery:从数据库加载数据并将其插入以选择,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4579570/

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