Creating a JSON dynamically with each input value using jquery

后端 未结 4 617
逝去的感伤
逝去的感伤 2020-12-02 04:49

I got a situation where I would like to read some data off a JSON format through PHP, however I am having some issues understanding how I should construct the Javascript obj

4条回答
  •  孤街浪徒
    2020-12-02 05:11

    May be this will help, I'd prefer pure JS wherever possible, it improves the performance drastically as you won't have lots of JQuery function calls.

    var obj = [];
    var elems = $("input[class=email]");
    
    for (i = 0; i < elems.length; i += 1) {
        var id = this.getAttribute('title');
        var email = this.value;
        tmp = {
            'title': id,
            'email': email
        };
    
        obj.push(tmp);
    }
    

提交回复
热议问题