JQuery - creating input with attributes from JSON

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

Ajax call:

$.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  />');
                });
            },

        });

Objects from JSON, however, have 5 additional properties in them, and not just Id and Title. When placing the debugger in a line with the creation of the input, the identifier and header are undefined.

How can I create these inputs from this JSON?

The returned JSON is 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
+4
source share
2 answers

JSON Data Data. $.each(data, $.each(data.Data,

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

jQuery, :

$.ajax({
  type: "GET",
  url: '@Url.Action("GetSubjects", "CommAPI")',
  dataType: "json",
  success: function (data) {
    $.each(data, function (index, element) {
      $input = $('<input'>).prop('id', element.Id).prop('title', element.title).addClass('k'); 
      $('#subjects').append($input);
    });
});

, , "Id" Javascript. idu, , .

:)

0

Source: https://habr.com/ru/post/1617734/


All Articles