Declare a JSON array of Objects and add items dynamically

后端 未结 4 617
心在旅途
心在旅途 2021-01-26 04:56

This is stupid but i am missing something and could not reach the conclusion. I am trying to initialize a JSON Array and trying to add JSON Objects to it run time. For an exampl

4条回答
  •  春和景丽
    2021-01-26 05:37

    Check this fiddle - http://jsfiddle.net/FamBn/1/

    HTML:

    
    
    
    

    JS:

    var employees=[
    {"firstName":"John", "lastName":"Doe"},
    {"firstName":"Anna", "lastName":"Smith"},
    {"firstName":"Peter", "lastName":"Jones"}
    ];
    
    $('#add').click(function(){
        var fname=$('#firstname').val();
        var lname=$('#lastname').val();
        employees.push({
            firstName: fname,
            lastName: lname
        });
        var output = '';
        for (var i=0; i

提交回复
热议问题