使用jQuery,PHP和MySQL加载JSON数据,用于单选按钮
我正在尝试填充第三组radiobuttons作为以下脚本的补充: http : //www.electrictoolbox.com/json-data-jquery-php-radio-buttons/
出于某种原因,我似乎无法用相应的数据填充第三组。 它只是留空:(
调用populateFruittype()
函数只返回[ ]
,而populateFruitVariety()
正确返回json数据。
getdata.php(数据库连接/获取数据)
prepare("SELECT DISTINCT variety FROM fruit WHERE name = ? ORDER BY variety"); $stmt->execute(array($_GET['fruitName'])); $rows[] = $stmt->fetchAll(PDO::FETCH_ASSOC); } if(isset($_GET['fruitVariety'] )) { $stmt = $pdo->prepare("SELECT DISTINCT fruittype FROM fruit WHERE variety = ? ORDER BY fruittype"); $stmt->execute(array($_GET['fruitVariety'])); $rows[] = $stmt->fetchAll(PDO::FETCH_ASSOC); } echo json_encode($rows); ?>
HTML
Toevoegen function populateFruitVariety() { var fruitName = $('input[name=fruitName]:checked').val(); $.getJSON('getdata.php', {fruitName: fruitName}, function(fruit) { var html = ''; $.each(fruit, function(index, array) { html = html + ' '; }); $('#varieties').html(html); }); } function populateFruittype() { var fruitVariety = $('input[name=fruitVariety]:checked').val(); $.getJSON('getdata.php', {fruitVariety: fruitVariety}, function(fruit) { var html = ''; $.each(fruit, function(index, array) { html = html + ' '; }); $('#fruittype').html(html); }); } $(function() { $('input[name=fruitName]').change(function() { populateFruitVariety(); }); }); $(function() { $('input[name=fruitVariety]').change(function() { populateFruittype(); }); });
数据库查询和内容可以在这里找到: http : //www.electrictoolbox.com/mysql-example-table/
只需添加:
`fruittype` varchar(50) NOT NULL
并用一些自定义值填充它。
问题解决了。
.change(function()
必须是.live('click', function()