我正在通过 jquery 和 json 填充选择框选项,但我不确定如何在我的表单中处理相同链接选择框的多个实例。
因为选择框仅在需要时呈现,所以某些记录将有十组链接选择框,而其他记录则只需要一个。如何生成唯一选择以支持辅助选择选项的自动填充?
这是我正在使用的代码,我提前感谢您提供的任何见解。
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript">
function populateACD() {
$.getJSON('/acd/acd2json.php', {acdSelect:$('#acdSelect').val()},
function(data) {
var select = $('#acd2');
var options = select.attr('options');
$('option', select).remove();
$.each(data, function(index, array) {
options[options.length] = new Option(array['ACD2']);
});
});
}
$(document).ready(function() {
populateACD();
$('#acdSelect').change(function() {
populateACD();
});
});
</script>
<?php
require_once('connectvars.php');
$dbc = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
$squery = "SELECT ACD1ID, ACD1 from ACD1";
$sdata = mysqli_query($dbc, $squery);
// Loop through the array of ACD1s, placing them into an option of a select
echo '<select name="acdSelect" id="acdSelect">';
while ($row = mysqli_fetch_array($sdata)) {
echo "<option value=" . $row['ACD1ID'] . ">" . $row['ACD1'] . "</option>\n";
}
echo '</select><br /><br />';
<select name="acd2" id="acd2">
</select>
acd2json.php
<?php
$dsn = "mysql:host=localhost;dbname=wfn";
$user = "acd";
$pass = "***************";
$pdo = new PDO($dsn, $user, $pass);
$rows = array();
if(isset($_GET['acdSelect'])) {
$stmt = $pdo->prepare("SELECT ACD2 FROM ACD2 WHERE ACD1ID = ? ORDER BY ACD2");
$stmt->execute(array($_GET['acdSelect']));
$rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
}
echo json_encode($rows);
?>