본문 바로가기
WEB/Javascript & jQuery

[Jquery] form, input 태그 동적 생성하기

by baam 2022. 5. 17.

동적으로 Form와 input 태그를 생성하여 처리를 하면 훨씬 깔끔해지는 상황들이 있다. 외워두고 쓰면 좋지만 블로그에 정리해두고 필요할 때마다 꺼내 쓰도록 하자.

 

	// Form 생성 및 속성 지정
	var newForm = $('<form></form>');
	
	newForm.attr("name","newForm");
	newForm.attr("method","post");
	newForm.attr("action","url");
	newForm.attr("target","_blank");

	// input 태그 생성
	newForm.append($('<input/>', {type: 'hidden', name: 'data1', value:'value1' }));
	newForm.append($('<input/>', {type: 'hidden', name: 'data2', value:'value2' }));

	// body에 추가
	newForm.appendTo('body');

	// submit
	newForm.submit();

 

댓글