리자

How to get and set form element values with jQuery

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

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건
+
제목 글쓴이 날짜 조회
09-07-11 조회 2,970
09-07-11 조회 3,203
09-06-29 조회 3,587
09-06-26 조회 5,209
09-06-25 조회 4,139
09-06-25 조회 2,810
09-06-18 조회 5,371
09-06-17 조회 2,713
09-06-15 조회 2,935
09-06-15 조회 2,566
09-06-15 조회 3,088
09-06-15 조회 2,978
09-06-15 조회 3,003
09-06-15 조회 2,852
09-06-09 조회 3,537
09-05-29 조회 3,140
09-05-29 조회 3,279
09-05-28 조회 3,580
09-05-22 조회 2,982
09-05-16 조회 3,825
09-05-14 조회 3,554
09-05-11 조회 5,915
09-05-08 조회 2,671
09-05-06 조회 2,371
09-05-05 조회 3,453
09-05-04 조회 3,287
09-05-01 조회 2,874
09-04-30 조회 3,423
09-04-30 조회 3,788
09-04-18 조회 2,714
09-04-17 조회 3,107
09-04-13 조회 3,306
09-04-13 조회 2,357
09-04-13 조회 3,102
09-04-13 조회 2,708
09-04-10 조회 3,238
09-04-10 조회 2,657
09-04-07 조회 3,659
09-04-04 조회 3,289
09-04-02 조회 7,263
09-03-19 조회 3,252
09-03-15 조회 5,122
09-03-15 조회 2,751
09-03-13 조회 3,055
09-03-10 조회 3,400
09-03-05 조회 3,953
09-03-05 조회 3,674
09-02-27 조회 4,201
09-02-23 조회 4,452
09-02-23 조회 4,443
09-02-21 조회 3,205
09-02-21 조회 2,832
09-02-17 조회 2,800
09-02-17 조회 3,188
09-02-17 조회 2,569
09-02-17 조회 3,363
09-02-17 조회 3,093
09-02-13 조회 2,878
09-02-09 조회 2,675
09-02-09 조회 3,524
09-02-09 조회 4,569
09-02-09 조회 3,641
09-02-09 조회 3,142
09-02-09 조회 2,903
09-02-05 조회 3,387
09-02-03 조회 6,147
09-01-30 조회 2,899
09-01-25 조회 3,570
09-01-24 조회 3,683
09-01-23 조회 3,850
09-01-23 조회 3,485
09-01-20 조회 5,867
09-01-20 조회 4,304
09-01-17 조회 4,307
09-01-17 조회 2,973
09-01-15 조회 2,553
09-01-14 조회 5,265
09-01-10 조회 3,088
09-01-09 조회 2,980
09-01-08 조회 3,495
09-01-08 조회 3,252
09-01-06 조회 2,929
09-01-05 조회 5,182
08-12-30 조회 3,080
08-12-30 조회 4,505
08-12-29 조회 4,355
08-12-27 조회 2,344
08-12-27 조회 3,210
08-12-27 조회 2,856
08-12-27 조회 3,150
08-12-20 조회 3,920
08-12-20 조회 4,299
08-12-13 조회 4,530
08-12-12 조회 4,598
08-12-12 조회 2,713
08-12-11 조회 2,716
08-12-10 조회 3,536
08-12-06 조회 3,282
08-11-30 조회 3,621
08-11-27 조회 4,175