리자

How to get and set form element values with jQuery

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

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건
+
제목 글쓴이 날짜 조회
10-05-31 조회 2,905
10-04-17 조회 4,696
10-03-23 조회 5,847
10-02-26 조회 3,364
10-02-23 조회 3,330
10-02-11 조회 3,947
10-02-09 조회 3,014
10-01-26 조회 3,468
10-01-23 조회 6,799
10-01-19 조회 3,503
10-01-13 조회 3,704
09-12-06 조회 4,640
09-11-24 조회 4,190
09-11-19 조회 3,064
09-11-11 조회 3,663
09-08-30 조회 3,145
09-08-12 조회 3,142
09-07-21 조회 3,323
09-07-19 조회 3,208
09-07-11 조회 3,126
09-06-15 조회 2,900
09-05-29 조회 3,074
09-05-28 조회 3,505
09-05-05 조회 3,363
09-04-07 조회 3,576
09-02-23 조회 4,373
09-02-17 조회 3,115
09-02-17 조회 2,494
09-02-09 조회 2,602
09-01-23 조회 3,408
09-01-17 조회 4,233
09-01-08 조회 3,186
08-11-07 조회 4,439
08-11-01 조회 4,927
08-10-30 조회 3,524
08-10-16 조회 3,440
08-10-02 조회 2,577
08-09-22 조회 3,932
08-09-22 조회 2,897
08-09-20 조회 5,395
08-09-20 조회 4,455
08-09-20 조회 3,526
08-09-20 조회 2,906
08-09-20 조회 3,102
08-09-20 조회 4,090
08-09-20 조회 3,574
08-09-20 조회 3,161
08-09-18 조회 3,407
08-09-06 조회 3,011
08-09-06 조회 2,755
08-09-05 조회 3,410
08-09-05 조회 5,619
08-09-03 조회 4,425
08-09-03 조회 2,681
08-07-31 조회 3,159
08-07-18 조회 3,761
08-06-19 조회 3,105
08-06-15 조회 3,098
08-06-14 조회 3,552
08-05-23 조회 2,789
08-05-13 조회 2,696
08-04-22 조회 4,375
08-04-22 조회 5,074
08-04-22 조회 6,814
08-04-17 조회 2,991
08-04-17 조회 3,575
08-04-17 조회 3,673
08-04-17 조회 5,914
08-04-17 조회 3,109
08-04-17 조회 3,442
08-04-06 조회 3,526
08-04-05 조회 3,105
08-04-04 조회 7,166
08-04-04 조회 4,454
08-04-04 조회 3,806
08-04-04 조회 3,985
08-04-04 조회 2,865
08-04-04 조회 2,651
08-04-04 조회 2,691
08-04-04 조회 2,888
08-04-04 조회 2,642
08-04-04 조회 2,614
08-04-04 조회 4,126
08-04-03 조회 2,648
08-04-03 조회 3,854
08-04-02 조회 3,077
08-04-02 조회 2,825
08-03-25 조회 3,681
08-03-11 조회 3,444
08-03-11 조회 2,952
08-03-11 조회 2,907
08-03-11 조회 2,707
08-03-11 조회 2,482
08-03-11 조회 2,287
08-03-11 조회 2,345
08-03-07 조회 2,886
08-03-05 조회 2,626
08-02-27 조회 2,854
08-02-21 조회 2,737
08-02-21 조회 3,310