locked
How to convert data from a form to JSON RRS feed

  • Question

  • User-1404740798 posted

    I want to convert the Value of the radio button on the form to Json and save it as a file.

    Can you tell me which part is the problem now that the last value is stored?

    <td class="center">
                                                <div class="radio pl-5">
                                                    <label>
                                                        <input type="radio" name="DoTeam_Medal1" id="DoTeam_Medal1" value="gold">
                                                        1등
                                                    </label>
                                                    <label>
                                                        <input type="radio" name="DoTeam_Medal1" id="DoTeam_Medal1" value="sliver">
                                                        2등
                                                    </label>
                                                    <label>
                                                        <input type="radio" name="DoTeam_Medal1" id="DoTeam_Medal1" value="bronze">
                                                        3등
                                                    </label>
                                                </div>
                                            </td>
    
    
            (function() {
                function toJSONString(form){
                    var obj = {};
                    var elements = form.querySelectorAll("input, select");
    
                    for(var i = 0; i < elements.length; ++i){
                        var element = elements[i];
                        var id = element.id;
                        var value = element.value;
    
                        if(id) obj[id] = value;
                    }
    
                    var obj_s = JSON.stringify(obj, null, "\t");
    
                    var dataUri = "data:application/json;charset=utf-8,"+ encodeURIComponent(obj_s);
                    var link = $("#link").attr("href", dataUri);
    
                    return JSON.stringify(obj, null, "\t");
                    
                }
    
                document.addEventListener( "DOMContentLoaded", function() {
    
    		        var form = document.getElementById( "info" );
    		        var output = document.getElementById( "output" );
    
    		        form.addEventListener( "submit", function( e ) {
    			        e.preventDefault();
    
    			        var json = toJSONString(this);
    			        output.innerHTML = json;
    
    		        }, false);
    
    	        });
    
    
            })();

    Monday, January 14, 2019 10:23 AM

Answers

  • User-474980206 posted

    normally you would use the name rather than the id. for checkbox and radio you check if checked:

    // handle checkbox, radio
    if (id && (["radio","checkbox"].indexOf(element.type) < 0 || element.checked)) 
       obj[id] = value;
    
    
    // should really include disabled for full compatability
    if (id && !element.disabled && (["radio","checkbox"].indexOf(element.type) < 0 || element.checked)) 
       obj[id] = value;
    

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, January 14, 2019 4:59 PM