리자

How to get and set form element values with jQuery

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

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,188건
+
제목 글쓴이 날짜 조회
07-07-16 조회 3,126
07-07-06 조회 3,012
07-07-06 조회 3,442
07-06-23 조회 3,270
07-06-16 조회 2,780
07-05-20 조회 4,287
07-05-09 조회 3,465
07-05-09 조회 6,103
07-05-04 조회 2,697
07-04-30 조회 9,832
07-04-27 조회 4,338
07-04-26 조회 3,449
07-04-25 조회 5,295
07-04-18 조회 7,792
07-04-13 조회 3,130
07-04-12 조회 6,640
07-04-12 조회 5,051
07-04-12 조회 3,359
07-04-11 조회 5,724
07-04-05 조회 6,369
07-03-24 조회 4,425
07-03-20 조회 3,747
07-03-18 조회 3,992
07-03-18 조회 4,740
07-03-17 조회 4,239
07-03-16 조회 4,134
07-03-15 조회 5,296
07-03-13 조회 5,404
07-03-13 조회 3,589
07-03-08 조회 4,501
07-03-08 조회 3,987
07-03-08 조회 3,298
07-03-08 조회 4,189
07-03-05 조회 4,448
07-03-04 조회 2,917
07-03-01 조회 5,949
07-02-28 조회 3,234
07-02-27 조회 4,411
07-02-26 조회 3,333
07-02-26 조회 3,041
07-02-23 조회 3,671
07-02-23 조회 3,643
07-02-22 조회 4,023
07-02-22 조회 3,834
07-02-19 조회 3,856
07-02-17 조회 4,876
07-02-14 조회 8,179
07-02-12 조회 3,948
07-02-10 조회 3,569
07-02-02 조회 3,817
07-01-31 조회 3,488
07-01-29 조회 3,347
07-01-25 조회 5,124
07-01-21 조회 3,272
07-01-19 조회 3,055
06-12-15 조회 4,068
06-12-15 조회 4,472
06-12-15 조회 2,962
06-12-15 조회 4,512
06-12-15 조회 3,400
06-12-15 조회 4,102
06-12-15 조회 3,047
06-12-15 조회 2,777
06-12-15 조회 4,638
06-12-15 조회 3,182
06-12-15 조회 3,286
06-12-07 조회 4,735
06-12-07 조회 4,020
06-12-04 조회 4,666
06-11-27 조회 3,712
06-11-21 조회 4,022
06-11-15 조회 3,453
06-11-04 조회 3,344
06-10-29 조회 3,999
06-10-13 조회 3,867
06-10-13 조회 4,227
06-10-13 조회 4,181
06-10-12 조회 5,372
06-10-12 조회 4,289
06-10-04 조회 3,790
06-10-02 조회 4,568
06-09-29 조회 3,056
06-09-26 조회 3,396
06-09-26 조회 3,658
06-09-26 조회 3,300
06-09-26 조회 3,451
06-09-26 조회 4,357
06-09-25 조회 4,662
06-09-22 조회 5,000
06-09-22 조회 3,565
06-09-21 조회 4,340
06-09-21 조회 4,344
06-09-21 조회 3,551
06-09-21 조회 5,201
06-09-21 조회 3,729
06-09-21 조회 3,459
06-09-20 조회 3,039
06-09-15 조회 3,167
06-09-13 조회 3,481
06-09-11 조회 3,285