gpt4 book ai didi

javascript - jQuery Ajax中的未定义数据返回

转载 作者:行者123 更新时间:2023-12-02 16:04:46 24 4
gpt4 key购买 nike

我正在使用jQuery Mobile,并且需要删除标签上方的undefined字。 data是json,它成功返回并几乎正确地构建了html。除了undefined我有这个动态代码

的HTML

<div class="ui-field-contain" id="selectNovoCorAnimal">
</div>

Java脚本
function corAnimal(data){
var html
html += '<label for="corNovoAnimal" class="select">Cor</label>'
html += '<select name="corNovoAnimal" id="corNovoAnimal" data-native-menu="false">'
$.each(data, function (i, x) {
html+='<option value='+ x.id + '>'+ x.descricao +'</option>';
});
html += '</select>'
$('#selectNovoCorAnimal').html(html).enhanceWithin();
}

像这样的构建:
undefined <<< need remove this
Cor
[Amarelo] << select

为什么给我不确定?

最佳答案

这是因为您没有给html变量一个初始值,所以该值是undefiend。当您尝试将其余的html字符串连接到它时,它将undefiend转换为字符串版本,并将其余的html添加到该字符串版本中。

只需设置一个初始值

var html = "";

关于javascript - jQuery Ajax中的未定义数据返回,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26790757/

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