从JSON中选择选项 [英] Select Options from JSON
问题描述
关于JSON,我是个新手,想尝试编写JSON选择选项自动填充,但不知道从哪里开始.
I am a newb when it comes to JSON and am wanting to try to write a JSON select option autofiller, but do not know where to start.
我的脚本当前的工作方式是使用PHP和MySQL用数据库表中的不同列表填充第一组选择选项,然后在用户选择后,下一组选择选项将自动填充与之链接的选项.第一组.无论如何,可以在JSON中执行此操作吗?
The way my script works currently is using PHP and MySQL to fill the first set of select options with a distinct list from a DB table and then upon a user selection the next set of select options are autofilled with options that are linked to the first set. Is there anyway to do this in JSON?
推荐答案
好的.假设您有一些简单的JSON:
Sure. Lets say you have some simple JSON:
{ "Options": [
{ "Text":"MyText","Value":"MyValue"},
{ "Text":"MyText2","Value":"MyValue2"}
]
}
然后,您将其评估为JavaScript:
Then, you evaluate that to JavaScript:
var options = eval('(' + myJson + ')'); // myJson is your data variable
然后,您只需在dom中创建每个选项(为简便起见,我将使用jQuery)
Then, you simply create each option in the dom (I'll use jQuery for brevity sake)
var length = options.length;
for(var j = 0; j < length; j++)
{
var newOption = $('<option/>');
newOption.attr('text', options[j].Text);
newOption.attr('value', options[j].Value); // fixed typo
$('#mySelect').append(newOption);
}
或类似的效果.
这篇关于从JSON中选择选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!