How to convert form data to object using MooTools

后端 未结 6 1829
借酒劲吻你
借酒劲吻你 2020-12-17 17:22

I would like to convert an entire form of data to a javascript object.

6条回答
  •  眼角桃花
    2020-12-17 17:44

    One way of doing it. -- Converting it to JSON object

    var hm = $('myform').toQueryString();
        hm = '{"'+hm+'"}'; 
        hm = hm.replace(/&/g, '","');
        hm = hm.replace(/=/g, '":"');
        var jsn = JSON.decode(hm); // jsn is ur JSON object.
    




    Convert it to Hash.

    Mootools has an object type called Hash. You can convert to that as well by doing the following.

    Hash link : http://mootools.net/docs/core/Native/Hash It has set and get methods and you can loop and do stuff, check the link.

    var hm = $('myform').toQueryString();

    var ar = hm.split('&');
    var finalo = new Hash();
    ar.each(function(a, aCounter)
    {
        var tmp = a.split('=');
        finalo.set(tmp[0], tmp[1]);
    });
    
    // finalo is your Hash object. Use the get() method to extract values. Check the link given above.
    

提交回复
热议问题