Programming Learning Diary

[Javascript/Jquery] form, input 동적 생성 및 submit

페이지 정보

작성일 20-08-03 16:04

본문


/* Javascript */
    // create element (form)
    var newForm = document.createElement('form');
    // set attribute (form) 
    newForm.name = 'newForm';
    newForm.method = 'post';
    newForm.action = 'koreanleo.com';
    newForm.target = '_blank';

    // create element (input)
    var input1 = document.createElement('input');
    var input2 = document.createElement('input');
    // set attribute (input)
    input1.setAttribute("type""hidden");
    input1.setAttribute("name""data1");
    input1.setAttribute("value""value1");
    input2.setAttribute("type""hidden");
    input2.setAttribute("name""data2");
    input2.setAttribute("value""value2");

    // append input (to form)
    newForm.appendChild(input1);
    newForm.appendChild(input2);

    // append form (to body)
    document.body.appendChild(newForm);
    
    // submit form
/* JQUERY */ 
    //create element (form)
    var newForm = $('<form></form>');
    //set attribute (form) 
    newForm.attr("name","newForm");
    newForm.attr("method","post");
    newForm.attr("action","koreanleo.com);
    newForm.attr("target","_blank");

    // create element & set attribute (input) 
    newForm.append($('<input/>', {type: 'hidden', name: 'data1', value:'value1' }));
    newForm.append($('<input/>', {type: 'hidden', name: 'data2', value:'value2' }));

    // append form (to body) 
    newForm.appendTo('body');

    // submit form
    newForm.submt();

댓글목록

등록된 댓글이 없습니다.