리자

How to get and set form element values with jQuery

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

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,902
10-04-17 조회 4,696
10-03-23 조회 5,846
10-02-26 조회 3,363
10-02-23 조회 3,328
10-02-11 조회 3,945
10-02-09 조회 3,014
10-01-26 조회 3,466
10-01-23 조회 6,796
10-01-19 조회 3,502
10-01-13 조회 3,702
09-12-06 조회 4,640
09-11-24 조회 4,189
09-11-19 조회 3,063
09-11-11 조회 3,661
09-08-30 조회 3,143
09-08-12 조회 3,142
09-07-21 조회 3,323
09-07-19 조회 3,206
09-07-11 조회 3,126
09-06-15 조회 2,899
09-05-29 조회 3,073
09-05-28 조회 3,503
09-05-05 조회 3,361
09-04-07 조회 3,574
09-02-23 조회 4,371
09-02-17 조회 3,114
09-02-17 조회 2,492
09-02-09 조회 2,601
09-01-23 조회 3,403
09-01-17 조회 4,230
09-01-08 조회 3,183
08-11-07 조회 4,437
08-11-01 조회 4,925
08-10-30 조회 3,524
08-10-16 조회 3,439
08-10-02 조회 2,575
08-09-22 조회 3,931
08-09-22 조회 2,895
08-09-20 조회 5,394
08-09-20 조회 4,453
08-09-20 조회 3,526
08-09-20 조회 2,905
08-09-20 조회 3,100
08-09-20 조회 4,088
08-09-20 조회 3,570
08-09-20 조회 3,158
08-09-18 조회 3,404
08-09-06 조회 3,008
08-09-06 조회 2,750
08-09-05 조회 3,408
08-09-05 조회 5,616
08-09-03 조회 4,425
08-09-03 조회 2,679
08-07-31 조회 3,158
08-07-18 조회 3,759
08-06-19 조회 3,104
08-06-15 조회 3,097
08-06-14 조회 3,547
08-05-23 조회 2,787
08-05-13 조회 2,695
08-04-22 조회 4,372
08-04-22 조회 5,073
08-04-22 조회 6,813
08-04-17 조회 2,988
08-04-17 조회 3,571
08-04-17 조회 3,672
08-04-17 조회 5,911
08-04-17 조회 3,105
08-04-17 조회 3,439
08-04-06 조회 3,525
08-04-05 조회 3,104
08-04-04 조회 7,165
08-04-04 조회 4,453
08-04-04 조회 3,806
08-04-04 조회 3,984
08-04-04 조회 2,862
08-04-04 조회 2,651
08-04-04 조회 2,690
08-04-04 조회 2,888
08-04-04 조회 2,642
08-04-04 조회 2,611
08-04-04 조회 4,123
08-04-03 조회 2,645
08-04-03 조회 3,852
08-04-02 조회 3,075
08-04-02 조회 2,823
08-03-25 조회 3,680
08-03-11 조회 3,443
08-03-11 조회 2,951
08-03-11 조회 2,906
08-03-11 조회 2,706
08-03-11 조회 2,480
08-03-11 조회 2,285
08-03-11 조회 2,344
08-03-07 조회 2,883
08-03-05 조회 2,622
08-02-27 조회 2,852
08-02-21 조회 2,735
08-02-21 조회 3,308