Creating a JSON dynamically with each input value using jquery

后端 未结 4 610
逝去的感伤
逝去的感伤 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 04:51

    I don't think you can turn JavaScript objects into JSON strings using only jQuery, assuming you need the JSON string as output.

    Depending on the browsers you are targeting, you can use the JSON.stringify function to produce JSON strings.

    See http://www.json.org/js.html for more information, there you can also find a JSON parser for older browsers that don't support the JSON object natively.

    In your case:

    var array = [];
    $("input[class=email]").each(function() {
        array.push({
            title: $(this).attr("title"),
            email: $(this).val()
        });
    });
    // then to get the JSON string
    var jsonString = JSON.stringify(array);
    

提交回复
热议问题