SCRIPT야

[펌]동적 폼/객체 생성

administrators 2011. 5. 16. 17:34
출처: http://codingholic.tistory.com/18
<script>
//#######################################################################
/*
    다이나믹 폼 생성
    DynamicFormManager
    2009-12-19 박용재.
    // 사용 예
    
    var myForm = new DynamicFormManager('get', 'b.jsp');
    myForm.addElement('test1', 'value1');
    myForm.addElement('test2', 'value2');
    myForm.addElement('test3', 'value3');
    myForm.addElement('test4', 'value4');
    myForm.addElement('test5', 'value5');
    
    필요에 따라서 action, method, name 등을 변경하여준다.
    //myForm.setName('sendForm');
    //myForm.setMethod('post');
    //myForm.setAction('abc.jsp');
    myForm.submit();
*/
//#######################################################################
function DynamicFormManager(method, action, name) {
    this.form = document.createElement('<form></form>');
    this.form.method = method;
    this.form.action = action;
    this.form.name = name ? name : 'DynamicForm';
    this.addElement = function(name, value) {
        if(!this.form) {
            alert('폼 생성이 안되었습니다');
            return;
        } else if(name == null || name == "") {
            alert('이름이 없습니다');
            return;
        }
        var input = document.createElement('<input></input>');
        input.type = 'hidden';
        input.name = name;
        input.value = value == null ? "" : value;
        this.form.appendChild( input );
        input = null;
    }
    this.make = function() {
        var div = document.createElement('<div></div>');
        div.style.display = 'none';
        div.appendChild(this.form);
        document.appendChild(div);
    }
    this.submit = function(target) {
        if(target == null || target == "") {
            var winName = 'submitWindow' + Math.random() * 10;
            winName = winName.substr(0, 13);
            var status = 'toolbar=no,location=no,status=no,menubar=no,scrollbars=no,resizable=no,width=500,height=500,top=0,left=0';
            var win = window.open('about:blank', winName, status);
            this.form.target = winName;
        } else {
            this.form.target = target;
        }
        this.make();
        this.form.submit();
    }
    this.setAction = function(action) {        this.form.action = action;    }
    this.getAction = function(action) {        return this.form.action;    }
    this.setMethod = function(method) {        this.form.method = method;    }
    this.getMethod = function() {        return this.form.method;    }
    this.setName = function(name) {        this.form.name = name;    }
    this.getName = function() {        return this.form.name;    }
}
//####################################################################### END
</script>

출처: http://codingholic.tistory.com/18