如何从 ajax 调用自动创建复选框(当前未定义)
Posted
技术标签:
【中文标题】如何从 ajax 调用自动创建复选框(当前未定义)【英文标题】:How to create checkbox automatic from ajax call (Undefined currently) 【发布时间】:2022-01-21 01:03:11 【问题描述】:您好,我需要一些帮助来自动为我的 ajax 名称创建一个复选框,我该怎么做?我已经在我的代码中添加了新的答案,但是目前我使用我实现的类型的复选框显示未定义
<div id="modal">
<section>
<div id="recyclable_id"></div>
</section>
</div>
选项的 Ajax 调用示例
$(document).ready(function()
$.ajax(
url: 'https://ecoexchange.dscloud.me:8090/api/get',
type: 'GET',
dataType: 'json',
headers:
query: "RecyclableTypeGet()",
// Gets the apikey from the sessionStorage
apikey: sessionStorage.getItem("apikey")
,
success: function(data)
console.log(data);
var html='';
$.each(data, function(key, value)
html +='<input type="checkbox" name="recyclable_id[]" value="'+value.RecyclableID+'"><label style="padding-left: 10px;">'+value.Name+'</label><br>';
);
$('#recyclable_id').html(html);
);
);
这是我将仅用于名称的 json 响应
[
"RecyclableID": 1,
"Name": "recyclable",
"RecyclableType": "test recyclable type"
,
"RecyclableID": 3,
"Name": "test recyclable 2",
"RecyclableType": "WASTE"
,
"RecyclableID": 129,
"Name": "test recyclable 4",
"RecyclableType": "test recyclable type"
,
"RecyclableID": 131,
"Name": "test recyclable 6",
"RecyclableType": "test recyclable type"
,
"RecyclableID": 132,
"Name": "test recyclable 7",
"RecyclableType": "test recyclable type"
,
"RecyclableID": 133,
"Name": "test recyclable 8",
"RecyclableType": "test recyclable type"
,
"RecyclableID": 134,
"Name": "test recyclable 34",
"RecyclableType": "WASTE"
,
"RecyclableID": 138,
"Name": "recyclable thing",
"RecyclableType": "WASTE"
,
"RecyclableID": 139,
"Name": "recyclable thing 2",
"RecyclableType": "Ewaste"
,
"RecyclableID": 153,
"Name": "test recyclable 10",
"RecyclableType": "Other"
,
"RecyclableID": 154,
"Name": "test recyclable 11",
"RecyclableType": "Ewaste"
,
"RecyclableID": 155,
"Name": "test recyclable 123",
"RecyclableType": "test recyclable type 2"
,
"RecyclableID": 159,
"Name": "some recyclable name",
"RecyclableType": "CC"
,
"RecyclableID": 161,
"Name": "some recyclable name 2",
"RecyclableType": "Ewaste"
,
"RecyclableID": 162,
"Name": "recyclable 2",
"RecyclableType": "test recyclable type 2"
,
"RecyclableID": 165,
"Name": "test recyclable 15",
"RecyclableType": "WASTE"
,
"RecyclableID": 166,
"Name": "test recyclable 18",
"RecyclableType": "testing type"
,
"RecyclableID": 167,
"Name": "some recyclable name 23",
"RecyclableType": "Ewaster"
]
这是我的图片显示未定义 我试图用给定的新分析器进行更改,但它似乎从类型得到未定义 x 10
【问题讨论】:
在你的循环中做一个console.log(value)
,这会告诉你你缺少什么以及是否有一个value.Name索引。
谢谢哦,奇怪的是又打电话给我的另一个 ajax 电话了吗?嗯
console.log() 是如何进行 ajax 调用的?
OHHHH 正在工作......我意识到我的查询调用错误地为我的选择选项一调用了相同的类型...... omg
【参考方案1】:
$.ajax(
type: "POST",
dataType: "json",
url: "../Service.asmx/GetSales",
success: function (response)
var result = response.d;
var dt = $.parseXML(result);
var serrows = '';
$.each(dt, function(key, value)
html +='<input type="checkbox" name="recyclable_id[]" value="'+value.RecyclableID+'"><label style="padding-left: 10px;">'+value.Name+'</label><br>';
)
【讨论】:
【参考方案2】:这行得通吗? 副驾驶创建了代码 xD
$(document).ready(function()
$.ajax(
url: 'http://localhost/recyclable/public/api/recyclable', type: 'GET', dataType: 'json', success: function(data)
console.log(data);
var html='';
$.each(data, function(key, value)
html +='<input type="checkbox" name="recyclable_id[]" value="'+value.RecyclableID+'"><label style="padding-left: 10px;">'+value.Name+'</label><br>';
);
$('#recyclable_id').html(html);
);
);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="recyclable_id"></div>
【讨论】:
我通过得到了未定义以上是关于如何从 ajax 调用自动创建复选框(当前未定义)的主要内容,如果未能解决你的问题,请参考以下文章