简体   繁体   English

如何使用MooTools将表单数据转换为对象

[英]How to convert form data to object using MooTools

I would like to convert an entire form of data to a javascript object. 我想将整个数据形式转换为javascript对象。

<form id='myform'>
   <input type='text' name='field1' value='foo'>
   <input type='text' name='field2' value='bar'>
</form>

would convert to a javascript object... 会转换为javascript对象...

{
   field1: 'foo',
   field2: 'bar'
}

In MooTools you can do easy trick how to convert all forms values into the Object: 在MooTools中,您可以轻松地将所有表单值转换为Object:

var formObjects=$('myform').toQueryString().parseQueryString();

Convert to JSON: 转换为JSON:

var formJson=JSON.encode(formObjects);

just write your own method, basing it upon the source of the Element.toQueryString - something like this (and i know the method name is rubbish but thats the least of your worries) 只需编写自己的方法,基于Element.toQueryString的源代码 - 就像这样(我知道方法名称是垃圾,但这是你最不担心的事情)

Element.implement({
    toJSON: function(){
        var json = {};
        this.getElements('input, select, textarea', true).each(function(el){
            if (!el.name || el.disabled || el.type == 'submit' || el.type == 'reset' || el.type == 'file') return;
            var value = (el.tagName.toLowerCase() == 'select') ? Element.getSelected(el).map(function(opt){
                return opt.value;
            }) : ((el.type == 'radio' || el.type == 'checkbox') && !el.checked) ? null : el.value;
            $splat(value).each(function(val){
                if (typeof val != 'undefined') {
                    json[el.name] = val;
                }
            });
        });
        return json;
    }
});

console.log($("myform").toJSON());

tested and working fine with the example form - http://mootools.net/shell/ZSsVr/ - produces the exact result you have asked for. 通过示例表单测试并正常工作 - http://mootools.net/shell/ZSsVr/ - 生成您要求的exact结果。

I actually like a combination of Dimitar Christoff answer and Trebla: 我其实喜欢Dimitar Christoff的回答和Trebla的组合:

Element.implement({
    toJSON: function(){
        var j = {};
        Array.each(this.toQueryString().split('&'),function(a){
            var kv = a.split('=')
            j[kv[0]] = kv[1]||'';
        });
        return JSON.encode(j);
    }
});
console.log($('formular_support').toJSON());

http://code.google.com/p/form2js/ http://code.google.com/p/form2js/

check this out, exactly what you're need, but framework independent 检查一下,确切地说你需要什么,但框架无关

MooTools doesn't come with a form serialization tool; MooTools没有表格序列化工具; i know, that sucks. 我知道,这很糟糕。

However, I've successfully used this stand-alone implementation: form2obj . 但是,我已经成功使用了这个独立的实现: form2obj

One way of doing it. 一种方法。 -- Converting it to JSON object - 将其转换为JSON对象

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. Mootools有一个名为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. 哈希链接: http//mootools.net/docs/core/Native/Hash它设置并获取方法,你可以循环和做东西,检查链接。

var hm = $('myform').toQueryString(); 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.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM