리자

How to get and set form element values with jQuery

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

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건
+
제목 글쓴이 날짜 조회
10-09-17 조회 2,354
10-09-16 조회 3,438
10-09-16 조회 2,161
10-09-16 조회 3,295
10-09-16 조회 2,237
10-09-16 조회 3,921
10-09-16 조회 2,176
10-09-16 조회 2,663
10-09-16 조회 2,178
10-09-15 조회 4,514
10-09-15 조회 2,868
10-09-15 조회 2,857
10-09-15 조회 2,862
10-09-15 조회 2,896
10-09-15 조회 2,646
10-09-15 조회 3,355
10-09-15 조회 2,851
10-09-15 조회 2,427
10-09-15 조회 4,287
10-09-15 조회 2,701
10-09-15 조회 2,917
10-09-15 조회 2,618
10-09-15 조회 3,521
10-09-15 조회 3,532
10-09-15 조회 3,191
10-09-15 조회 2,547
10-09-15 조회 2,747
10-09-15 조회 2,412
10-09-15 조회 2,421
10-09-15 조회 2,616
10-09-14 조회 4,135
10-09-11 조회 4,073
10-09-11 조회 3,006
10-09-11 조회 2,518
10-09-11 조회 2,711
10-09-11 조회 2,697
10-09-11 조회 2,576
10-09-11 조회 2,489
10-09-11 조회 2,389
10-09-11 조회 2,738
10-09-11 조회 2,836
10-09-11 조회 2,465
10-09-11 조회 2,462
10-09-11 조회 2,647
10-09-11 조회 3,116
10-09-11 조회 2,626
10-09-08 조회 3,005
10-09-08 조회 2,738
10-09-07 조회 3,515
10-09-07 조회 4,575
10-09-07 조회 3,900
10-09-07 조회 2,451
10-09-07 조회 3,433
10-09-07 조회 3,725
10-09-04 조회 2,990
10-08-29 조회 2,878
10-08-27 조회 4,180
10-08-27 조회 4,187
10-08-20 조회 7,060
10-08-06 조회 2,975
10-07-30 조회 5,739
10-07-27 조회 2,841
10-07-15 조회 3,180
10-07-06 조회 3,256
10-07-01 조회 3,134
10-07-01 조회 2,657
10-07-01 조회 2,890
10-07-01 조회 3,146
10-06-30 조회 3,036
10-06-30 조회 3,908
10-06-30 조회 2,901
10-06-30 조회 3,010
10-06-30 조회 2,469
10-06-30 조회 2,595
10-06-30 조회 2,612
10-06-30 조회 2,539
10-06-30 조회 2,539
10-06-30 조회 2,568
10-06-30 조회 2,708
10-06-30 조회 2,728
10-06-30 조회 4,135
10-06-30 조회 2,481
10-06-30 조회 3,158
10-06-30 조회 3,065
10-06-17 조회 3,167
10-06-16 조회 2,863
10-06-14 조회 3,432
10-06-13 조회 2,123
10-06-11 조회 2,776
10-06-11 조회 2,678
10-06-11 조회 4,390
10-06-11 조회 4,773
10-06-11 조회 3,463
10-06-01 조회 2,956
10-05-31 조회 3,659
10-05-31 조회 3,077
10-05-31 조회 2,640
10-05-22 조회 5,313
10-05-18 조회 2,996
10-05-18 조회 3,026