Php Can';t使用Jquery获取JSON值
我做错了什么,但我似乎不知道是什么 $.getJSON行应该通过AJAX请求检索数据,并将当前选择的水果名称传递给它。然后,数据被循环(它是一个包含包含水果品种名称的索引“variation”的数组)并添加到html字符串中,最后由Jquery注入到网页的“variates span”中,替换之前的内容 HTMLPhp Can';t使用Jquery获取JSON值,php,jquery,mysql,ajax,json,Php,Jquery,Mysql,Ajax,Json,我做错了什么,但我似乎不知道是什么 $.getJSON行应该通过AJAX请求检索数据,并将当前选择的水果名称传递给它。然后,数据被循环(它是一个包含包含水果品种名称的索引“variation”的数组)并添加到html字符串中,最后由Jquery注入到网页的“variates span”中,替换之前的内容 HTML <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" conte
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<title>Toevoegen</title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js"></script>
<script language="javascript" type="text/javascript">
function populateFruitVariety() {
var fruitName = $('input[name=fruitName]:checked').val();
$.getJSON('func.php', {fruitName: fruitName}, function(fruit) {
var html = '';
$.each(fruit[fruitName], function(index, array) {
html = html + '<label><input type="radio" name="variety" value="' + array['variety'] + '" />' + array['variety'] + '</label> ';
});
$('#varieties').html(html);
});
}
populateFruitVariety();
$('input[name=fruitName]').change(function() {
populateFruitVariety();
});
</script>
</head>
<body>
<form>
<div>
<strong>Fruit:</strong>
<label><input type="radio" name="fruitName" value="Apple" checked="checked" />Apple</label>
<label><input type="radio" name="fruitName" value="Banana" />Banana</label>
<label><input type="radio" name="fruitName" value="Orange" />Orange</label>
<label><input type="radio" name="fruitName" value="Pear" />Pear</label>
</div>
<div>
<strong>Variety:</strong>
<span id="varieties"></span>
</div>
<div>
<strong>Type:</strong>
<span id="type"></span>
</div>
</form>
这些值将在控制台中检索,但不会显示为RadioBox。不过控制台中仍然有一个错误。(a未定义)我无法测试,但如果您更改-
$.each(fruit[fruitName], function(index, array) {
到
它有用吗?请注意使用这些教程的每个人,它们都很旧!使用jQuery1.4.2——我花了一天多的时间试图让一个简单的
selectbox
工作(,newoption()
在1.7.2中似乎不起作用),当我切换到jQuery1.4.2时,jQuery1.7.2不起作用
在上面的示例中,jroen使用的是jQuery 1.6.1,这可能就是问题所在。Wooh这似乎很有效!但是,仅当根据本教程使用:$(function()时,才可以使用水果[fruitName],而不使用$(function(),请参见:
$(function() {
populateFruitVariety();
$('input[name=fruitName]').change(function() {
populateFruitVariety();
});
});
$.each(fruit[fruitName], function(index, array) {
$.each(fruit, function(index, array) {