jQuery is a Javascript framework which can simplify coding Javascript for a website and removes a lot of cross browser compatibility issues. This post looks at how to get and set the values of HTML form elements with jQuery.
Getting a form value with jQuery
To get a form value with jQuery use the val() function. Let's say we have a form like this, using an id for each form element:
<input name="foo" id="foo" type="text">
<select name="foo" id="bar">
<option value="1">one</option>
<option value="2">two</option>
<option value="3">three</option>
</select>
We can display an alert for the values of "foo" and "bar" as easily this:
window.alert( $('#foo').val() );
window.alert( $('#bar').val() );
If we're using the name only and not specifying an id, the jQuery to get the form values would be this:
window.alert( $('[@name=foo]').val() );
window.alert( $('[@name=bar]').val() );
If you have a group of radio buttons and want to get the selected button, the code is slightly different because they all have the same name. Using the above code examples will show the value for the first radio button on the form with that name. To find out the value of the
HTML:
<input type="radio" name="baz" value="x"> <input type="radio" name="baz" value="y"> <input type="radio" name="baz" value="z">
jQuery:
window.alert($('input[@name=baz]:checked ').val());
Setting a form value with jQuery
You can set the form values with jQuery using the same val() function but passing it a new value instead. Using the same example forms above, you'd do this for the text input and select box:
$('#foo').val('this is some example text');
$('#bar').val('3');
OR
$('[@name=foo]').val('this is some example text');
$('[@name=bar]').val('3');
Using the above for a radio button will change the actual value of the radio button rather than changing the one that is selected. To change the radio button that is selected you'd do this:
$('input[@name="baz"]')[0].checked = true;
[0] would set the first one
게시글 목록
| 번호 | 제목 |
|---|---|
| 12307 |
JavaScript
테이블 각 row 마우스오버시 색깔바꾸기 입니다.
|
| 12306 |
JavaScript
png 24비트 투명처리하기 입니다.
|
| 12305 |
JavaScript
full-down메뉴 자바스크립트 입니다.
|
| 12304 |
JavaScript
iframe사용시 높이 자동 조정하기 입니다.
|
| 12303 |
JavaScript
계산기
|
| 12302 |
JavaScript
숫자를 한글로 변환하기 입니다.
|
| 12301 |
JavaScript
아이프레임 자동으로 늘어나는 자바 스크립트 입니다.
|
| 12300 |
JavaScript
끊기지 않는 배너 롤링 입니다.
|
| 12299 |
기타
랜섬에 당한 화일 복구
|
| 12298 |
JavaScript
id값 변수명으로 치환 예제 자바스크립트 입니다.
|
| 12297 |
JavaScript
상태바의 링크 주소 감추기 입니다.
|
| 12296 |
JavaScript
가짜 카운터 달기 입니다.
|
| 12295 |
JavaScript
홈에 머문시간을 제목표시줄과 상태바에 실시간으로 알려주기 입니다.
|
| 12294 |
JavaScript
prompt 와 alert 응용 자바스크립트 예제 입니다.
|
| 12293 |
JavaScript
prompt 와 alert 간단한 자바스크립트 예제 입니다.
|
| 12292 |
jQuery
간단한 탭 제이쿼리
|
| 12291 |
jQuery
간단한 셀렉트 드롭다운 제이쿼리
|
| 12290 |
JavaScript
문서내 이미지갯수 출력하기 입니다.
|
| 12289 |
JavaScript
내용 복사해서 붙어넣기 하면 출처 따라오는 자바스크립트입니다.
|
| 12288 |
JavaScript
팝업창 지정한 시간대에 예약 열기 입니다.
|
| 12287 |
JavaScript
접속때마다 음악 다르게 나오기 입니다.
|
| 12286 |
JavaScript
자동 스크롤 메뉴 관련 입니다.
|
| 12285 |
JavaScript
이 브라우저로 몇페이지나 보았나 확인하기 입니다.
|
| 12284 |
JavaScript
사용자 브라우저의 펄러그인 목록 출력하기 입니다.
|
| 12283 |
JavaScript
상태표시줄 제어 + 간단한 복사 제어 하기 입니다.
|
댓글 작성
댓글을 작성하시려면 로그인이 필요합니다.
로그인하기