Javascript 의 form 의 reset() 메소드는 hidden field 와 check, radio button 에 대해 초기화를 시켜주지 않는다. 따라서 form 의 모든 field 를 초기화 시키려면 아래의 메소드가 필요하다.
소스
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | $.fn.clearForm = function () { return this.each(function () { var type = this.type, tag = this.tagName.toLowerCase(); if (tag === 'form') { return $(':input', this).clearForm(); } if ( type === 'text' || type === 'password' || type === 'hidden' || tag === 'textarea' ) { this.value = ''; } else if (type === 'checkbox' || type === 'radio') { this.checked = false; } else if (tag === 'select') { this.selectedIndex = -1; } }); }; |
예제
1 | $('#form').clearForm(); |
설명
여기의 clearForm 메소드를 hidden 도 초기화할 수 있게 커스터마이징 했다.