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
게시글 목록
| 번호 | 제목 |
|---|---|
| 12333 |
JavaScript
한글만 되게 하는 자바스크립트 입니다.
|
| 12332 |
JavaScript
간단한 팝업창 중앙에 띄우기 입니다.
|
| 12331 |
JavaScript
2단계 드롭다운 레이어 메뉴 입니다.
|
| 12329 |
JavaScript
동적 막대그래프
1
|
| 12328 |
JavaScript
간단한 위로가기 자바스크립트 입니다.
|
| 12327 |
JavaScript
따라다니는 link 퀵메뉴 입니다.
|
| 12326 |
JavaScript
글씨의 색을 자동으로 변환시켜주는 자바스크립트 입니다.
|
| 12325 |
JavaScript
인스타그램 사진가져오기
|
| 12324 |
jQuery
checkedbox 컨트롤
|
| 12323 |
jQuery
radio 컨트롤
|
| 12322 |
jQuery
select box 컨트롤
|
| 12321 | |
| 12320 |
node.js
Node.js - 실시간 위치 추적 (2)
|
| 12319 | |
| 12318 |
JavaScript
특정 이메일 입력 금지및 이메일 유효성 검사하기 입니다.
|
| 12317 |
JavaScript
마우스 오버시 그림 변하게 하는 자바스크립트 입니다
|
| 12316 |
JavaScript
시작과 멈춤이 가능한 스크롤 텍스트 입니다.
|
| 12315 |
JavaScript
이미지 없이 라운딩 박스 + 보더 표현하기 입니다.
|
| 12314 |
JavaScript
Trim 함수 자바스크립트로 사용하기 입니다.
|
| 12313 |
JavaScript
실제 이미지 크기 알아내기 입니다.
|
| 12312 |
JavaScript
present 자바스크립트 입니다.
|
| 12311 |
JavaScript
이미지 미리 로딩시켜놓기 입니다.
|
| 12310 |
JavaScript
다각형 이미지슬라이드쇼 자바스크립트 입니다.
|
| 12309 |
JavaScript
가운데 정렬된 홈페이지에서 따라다니는 레이어 자바스크립트 입니다.
|
| 12308 |
JavaScript
클릭시 펼쳐지는 메뉴 입니다.
|
댓글 작성
댓글을 작성하시려면 로그인이 필요합니다.
로그인하기