0
我想填充使用json和PHP的HTML页面中的选择框,但是当我点击选择框时,它只是返回一个空白选项。什么是错的?如何使用json填充选择框?
HTML代码:
Choice a problem:
<select name="notification" id="notification" class="form-control">
<option value="00">Select...</option>
</select>
PHP代码:
header('Access-Control-Allow-Origin: *');
include 'dbconfig.php';
try{
$results = $DB_con->prepare("SELECT idproblema, tipoproblema FROM problema ORDER BY idproblema");
$results->execute();
$res = $results->fetchAll(PDO::FETCH_ASSOC);
$hold = array();
foreach($res as $reg){
$hold[] = array('idproblema' => $reg['idproblema'], 'tipoproblema' =>$reg['tipoproblema']);
}
}
catch (Exception $e) {
echo "Data could not be retrieved from the database.";
exit();
}
echo json_encode($hold);
jQuery代码:
$(document).ready(function(){
jQuery.support.cors = true;
$('#notification').click(function(){
$.ajax({
url : 'url.php',
type : 'GET',
data : "json",
crossDomain: true,
success: function (data) {
if (data.length > 0) {
$.each(data, function (index, value) {
$('#notification').append('<option value="' + value.idproblema+ '">' + value.tipoproblema + '</option>');
});
}
else {
var newOption= $('<option value=""></option>');
$('#notification').append(newOption);
}
}
});
});
})
................ ...............................
您在输入字段ID中输入了错字。它叫“notificaton”而不是“通知” - 错过我,所以jquery不会工作 – Tamar
@Tamar,当我打破代码时,这是一个错误。 TKS – Leonardo