Javascript 无法读取属性';长度';未定义的
我正在修改选择的下拉菜单。为此,我使用jquery、ajax和jsonJavascript 无法读取属性';长度';未定义的,javascript,php,jquery,ajax,json,Javascript,Php,Jquery,Ajax,Json,我正在修改选择的下拉菜单。为此,我使用jquery、ajax和json function ChooseGroupScript() { $.getJSON('edit_scripts.php', {group:$('#group').val()}, function(data) { var select = $('#subject'); var options
function ChooseGroupScript() {
$.getJSON('edit_scripts.php', {group:$('#group').val()}, function(data) {
var select = $('#subject');
var options = select.attr('options');
$('option', select).remove();
$.each(data, function(index, array) {
options[options.length] = new Option(array['subject']);
});
});
}
$(document).ready(function() {
ChooseGroupScript();
$('#group').change(function() {
ChooseGroupScript();
});
});
我签入控制台:我收到如下错误:
Jquery type error on cannot read property 'length' of undefined
这是我的PHP:
if(isset($_GET['group'])){
$currentscript = $clients->curr_group_content($_GET['group']);
$scriptscount = sizeof($currentscript);
for($i=0;$i<$scriptscount;$i++){
$subject = $currentscript[$i]['subject'];
}
}
echo json_encode($subject);
if(isset($\u GET['group'])){
$currentscript=$clients->curr\u group\u content($\u GET['group']);
$scriptscont=sizeof($currentscript);
for($i=0;$i选项不是select元素上的属性。您可能只是想直接将新选项添加到元素中:
var select = $('#subject');
// remove this: var options = select.attr('options');
$('option', select).remove(); // or select.empty() to clean it out
$.each(data, function(index, array) {
select.append(new Option(array['subject']));
});
在您的代码中
var options = select.attr('options');
这是不对的,因为它不是一个属性。获取所有选项的正确方法
var options = $('#subject options');
在循环中
可以
var select = $('#subject').empty();
$.each(data, function(index, val) {
select.append($('<option/>', { 'value':index, 'text':val }));
});
var select=$('#subject').empty();
$.each(数据、函数(索引、val){
select.append($('',{'value':index,'text':val}));
});
也向我们展示您的html代码我尝试了您描述的方式。我解决了问题,但没有获得数据。原因可能是什么?您能发布JSON吗?您的PHP毫无意义。您正在覆盖变量,而不是创建数组。
var select = $('#subject').empty();
$.each(data, function(index, val) {
select.append($('<option/>', { 'value':index, 'text':val }));
});