리자

How to get and set form element values with jQuery

· 2008-11-19 (수) 17:45:43 · 4036

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 checked one, do this instead:

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 checked, [1] would set the second one checked and so on.

[이 게시물은 관리자님에 의해 2011-10-31 16:55:28 jQuery에서 이동 됨]
댓글을 작성하시려면 로그인이 필요합니다.

프로그램

8,187건
+
제목 글쓴이 날짜 조회
09-10-03 조회 3,188
08-02-03 조회 3,492
08-02-03 조회 3,429
08-01-30 조회 4,945
08-01-29 조회 2,813
08-01-27 조회 3,949
08-01-27 조회 2,888
08-01-27 조회 3,168
08-01-26 조회 8,713
08-01-26 조회 3,266
08-01-24 조회 5,320
08-01-23 조회 3,661
08-01-21 조회 3,698
08-01-21 조회 3,448
08-01-21 조회 3,471
08-01-18 조회 3,983
08-01-18 조회 4,618
08-01-18 조회 6,780
08-01-18 조회 4,057
08-01-17 조회 4,044
08-01-17 조회 3,875
08-01-16 조회 3,127
08-01-15 조회 3,506
08-01-14 조회 6,758
08-01-12 조회 4,360
08-01-12 조회 3,921
08-01-12 조회 4,636
08-01-12 조회 3,879
08-01-12 조회 4,173
08-01-11 조회 7,205
08-01-10 조회 3,949
08-01-08 조회 3,481
08-01-08 조회 3,962
08-01-06 조회 2,745
08-01-04 조회 1.1만
08-01-02 조회 5,828
07-12-28 조회 4,762
07-12-24 조회 3,177
07-12-21 조회 4,880
07-12-17 조회 3,662
07-12-14 조회 5,031
07-12-13 조회 6,467
07-12-10 조회 5,109
07-12-10 조회 3,985
07-12-10 조회 4,385
07-12-08 조회 3,740
07-12-07 조회 4,073
07-12-07 조회 3,204
07-12-06 조회 4,951
07-12-04 조회 3,536
07-12-04 조회 3,683
07-12-03 조회 3,440
07-12-03 조회 4,463
07-12-02 조회 3,292
07-11-30 조회 4,041
07-11-30 조회 5,270
07-11-30 조회 5,872
07-11-29 조회 3,720
07-11-27 조회 4,172
07-11-27 조회 5,950
07-11-27 조회 4,049
07-11-26 조회 3,173
07-11-23 조회 4,628
07-11-23 조회 4,359
07-11-22 조회 5,268
07-11-21 조회 3,484
07-11-21 조회 3,607
07-11-21 조회 1.2만
07-11-20 조회 4,991
07-11-16 조회 4,844
07-11-13 조회 4,511
07-11-12 조회 4,239
07-11-10 조회 5,617
07-11-08 조회 6,778
07-11-08 조회 5,106
07-11-07 조회 4,576
07-11-07 조회 5,025
07-11-06 조회 3,569
07-11-06 조회 3,928
07-11-05 조회 5,262
07-11-02 조회 4,150
07-11-01 조회 5,614
07-10-31 조회 3,301
07-10-30 조회 3,769
07-10-29 조회 3,768
07-10-26 조회 5,084
07-10-25 조회 5,173
07-10-25 조회 3,950
07-10-24 조회 3,944
07-10-24 조회 4,261
07-10-24 조회 4,120
07-10-24 조회 3,395
07-10-24 조회 3,515
07-10-23 조회 6,164
07-10-23 조회 5,550
07-10-22 조회 4,277
07-10-22 조회 3,657
07-10-22 조회 4,199
07-10-22 조회 3,778
07-10-19 조회 4,829