gpt4 book ai didi

javascript - 未捕获的类型错误 : Cannot read property 'length' of null JSON-JQUERY

转载 作者:行者123 更新时间:2023-12-01 05:51:28 26 4
gpt4 key购买 nike

其功能有问题并在控制台显示未捕获的类型错误:无法读取 null 的属性“长度”并且无法理解为什么

function shownameplaylist(){
$.getJSON( "js/json/nameplaylist.json", function( data ) {
var items = [];
$.each( data, function( key, value ) {
$('#nameplaylist').append('<option>'+value.title+'</option>')
});
$('.nameplaylist option:first-child').attr("selected", "selected");
nameplaylist=$( ".nameplaylist option:selected" ).text();
console.log(nameplaylist.length);
if(nameplaylist.length==0){
$('#nameplaylist').css('display','none');
$('#delplaylist').css('display','none');
}
}).done(function(){ // If the AJAX call encountered an error
showplaylist();
})
.fail(function(){ // If the AJAX call encountered an error
console.log('no load nameplaylist');
$('#nameplaylist').css('display','none');
$('#delnamelist').css('display','none');
$('#delplaylist').css('display','none');

});
}

最佳答案

您对“nameplaylist”使用了两个不同的选择器:#nameplaylist.nameplaylist。第一个表示 ID 为“nameplaylist”的元素,第二个表示 class 为“nameplaylist”的元素。

$('#nameplaylist')$('.nameplaylist') 不会选择相同的元素,除非您的元素同时具有 id “nameplaylist”的:

<elem id="nameplaylist" />       <!-- $('#nameplaylist') -->
<elem class="nameplaylist" /> <!-- $('.nameplaylist') -->

确定您实际使用的选择器并相应地修改您的选择器。

看看你的代码,我想你的修复方法只是将第 7 行和第 8 行更改为:

$('#nameplaylist option:first-child').attr("selected", "selected");
nameplaylist=$( "#nameplaylist option:selected" ).text();

关于javascript - 未捕获的类型错误 : Cannot read property 'length' of null JSON-JQUERY,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21824918/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com