JQuery - 使用JSON中的属性创建输入 [英] JQuery - Create input with attributes from JSON

查看:90
本文介绍了JQuery - 使用JSON中的属性创建输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用从JSON设置的id和value属性创建输入。我有一个获取JSON的ajax调用,返回的数据很好,对于JSON中的每个对象,我想创建一个带有ID和JSON值的按钮。

I am trying to create an input with the id and value attributes set from a JSON. I have an ajax call which gets the JSON and the data returned is just fine, and for each object from the JSON I want to create a button with an ID and a value from the JSON.

Ajax电话:

$.ajax({
            type: "GET",
            url: '@Url.Action("GetSubjects", "CommAPI")',
            dataType: "json",
            success: function (data) {
                $.each(data, function (index, element) {
                    $('#subjects').append('<input type ="button" id=" ' + element.Id + ' " value=' + element.Title + '  class=k-button  />');
                });
            },

        });

然而,JSON中的对象还有5个附加属性,而不仅仅是Id和Title。将调试器放置在创建输入的行时,Id和Title未定义。

The objects from the JSON however have 5 additional properties in them, not just Id and Title. When placing the debugger at the line with the input creation, the Id and Title are undefined.

如何从此JSON创建这些输入?

How may I create these inputs from this JSON?

从控制台复制的返回JSON:

Returned JSON copied from the Console:

14:41:57.928 {"Data":[{"Id":1,"IdCSite":1,"IdDEvent":1,"Title":"Test","CaseName":null,"Description":"dsadasdasda","InsertedDate":"/Date(-62135596800000)/","SelectedUsers":null,"ViewSelectedUsers":null,"IsCurrentUserIncluded":false},{"Id":2,"IdCSite":1,"IdDEvent":1,"Title":"Test2","CaseName":null,"Description":"sdadasdas","InsertedDate":"/Date(-62135596800000)/","SelectedUsers":null,"ViewSelectedUsers":null,"IsCurrentUserIncluded":false},{"Id":3,"IdCSite":1,"IdDEvent":1,"Title":"test 3","CaseName":null,"Description":"sdadasdasda","InsertedDate":"/Date(-62135596800000)/","SelectedUsers":null,"ViewSelectedUsers":null,"IsCurrentUserIncluded":false}],"Total":3,"AggregateResults":null,"Errors":null}1 messageboard:128:25


推荐答案

根据您的JSON,您需要使用数据的数据属性对象。因此改变 $。each(data, to $。each(data.Data,

As per your JSON, you need to use Data property of data object. thus change $.each(data, to $.each(data.Data,

 $.each(data.Data, function (index, element) {
     $('#subjects').append('<input type ="button" id="' + element.Id + '"  value="' + element.Title + '"  class=k-button  />');
 });

这篇关于JQuery - 使用JSON中的属性创建输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆