리자

How to get and set form element values with jQuery

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

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건
+
제목 글쓴이 날짜 조회
08-10-30 조회 2,699
08-10-30 조회 2,793
08-10-20 조회 2,527
08-10-11 조회 2,426
08-09-20 조회 4,174
08-09-20 조회 2,841
08-09-18 조회 2,718
08-09-18 조회 3,420
08-09-17 조회 3,288
08-09-05 조회 4,629
08-09-03 조회 2,754
08-08-18 조회 2,893
08-08-17 조회 2,630
08-08-13 조회 3,411
08-07-30 조회 3,470
08-07-28 조회 4,350
08-07-22 조회 5,582
08-07-22 조회 4,062
08-07-10 조회 3,033
08-07-08 조회 3,095
08-06-30 조회 3,856
08-06-30 조회 3,340
08-06-30 조회 3,657
08-06-14 조회 2,726
08-06-11 조회 7,089
08-06-05 조회 3,305
08-05-20 조회 4,821
08-05-13 조회 4,155
08-05-08 조회 3,108
08-05-08 조회 5,263
08-04-25 조회 4,061
08-04-22 조회 4,525
08-04-21 조회 3,167
08-04-17 조회 2,553
08-04-17 조회 2,951
08-04-17 조회 2,251
08-04-17 조회 5,220
08-04-17 조회 4,540
08-04-17 조회 4,529
08-04-17 조회 3,315
08-04-17 조회 3,141
08-04-17 조회 3,629
08-04-17 조회 4,992
08-04-17 조회 5,139
08-04-17 조회 3,346
08-04-04 조회 4,605
08-04-04 조회 2,828
08-04-04 조회 2,572
08-04-04 조회 2,805
08-04-04 조회 2,541
08-04-04 조회 2,776
08-04-03 조회 2,664
08-04-03 조회 2,732
08-04-02 조회 3,048
08-04-02 조회 3,199
08-03-31 조회 3,094
08-03-26 조회 4,269
08-03-11 조회 2,955
08-03-11 조회 2,366
08-03-11 조회 2,375
08-03-07 조회 4,208
08-03-07 조회 3,302
08-03-06 조회 4,756
08-03-04 조회 3,388
08-02-19 조회 3,782
08-02-03 조회 3,406
08-01-18 조회 3,989
08-01-15 조회 3,442
08-01-12 조회 4,290
08-01-12 조회 3,853
08-01-12 조회 3,805
07-12-24 조회 3,101
07-11-30 조회 3,936
07-11-08 조회 4,996
07-11-06 조회 3,853
07-10-26 조회 5,000
07-10-24 조회 4,186
07-10-22 조회 3,572
07-10-19 조회 4,752
07-10-09 조회 4,891
07-10-09 조회 6,754
07-10-05 조회 3,086
07-10-05 조회 4,022
07-10-03 조회 7,535
07-10-03 조회 3,579
07-10-03 조회 2,929
07-10-03 조회 2,853
07-09-28 조회 4,698
07-09-27 조회 3,765
07-09-27 조회 3,216
07-09-13 조회 3,363
07-08-28 조회 3,452
07-08-04 조회 4,836
07-08-01 조회 5,528
07-08-01 조회 4,937
07-07-30 조회 5,450
07-07-29 조회 3,599
07-07-21 조회 3,422
07-07-18 조회 5,379
07-07-16 조회 3,179