리자

How to get and set form element values with jQuery

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

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건
+
제목 글쓴이 날짜 조회
12-08-29 조회 4,164
12-08-07 조회 3,822
12-08-03 조회 3,642
12-07-31 조회 3,744
12-07-30 조회 3,698
12-07-16 조회 3,895
12-07-15 조회 3,729
12-07-07 조회 3,590
12-07-06 조회 3,799
12-07-03 조회 3,751
12-06-29 조회 3,363
12-06-22 조회 3,737
12-06-17 조회 3,995
12-05-21 조회 3,919
12-05-02 조회 3,445
12-05-02 조회 4,241
12-05-02 조회 3,362
12-04-30 조회 3,559
12-04-30 조회 3,239
12-03-17 조회 4,100
12-03-10 조회 3,436
12-03-08 조회 3,958
12-03-03 조회 3,675
12-02-27 조회 5,086
12-02-17 조회 4,385
12-02-16 조회 4,666
12-02-03 조회 3,333
12-01-31 조회 3,240
12-01-24 조회 3,239
12-01-17 조회 3,942
12-01-05 조회 3,486
11-12-30 조회 3,220
11-12-23 조회 4,775
11-12-08 조회 3,536
11-12-07 조회 4,503
11-11-27 조회 3,923
11-11-25 조회 4,252
11-11-23 조회 3,703
11-11-22 조회 3,558
11-11-09 조회 3,265
11-10-23 조회 3,205
11-10-19 조회 3,156
11-10-08 조회 2,842
11-10-04 조회 2,842
11-10-04 조회 5,302
11-09-01 조회 4,373
11-08-13 조회 2,559
11-08-06 조회 3,237
11-07-11 조회 3,632
11-06-27 조회 2,993
11-06-21 조회 2,656
11-06-13 조회 2,466
11-06-07 조회 2,477
11-05-26 조회 2,434
11-04-20 조회 2,759
11-03-21 조회 2,861
11-03-18 조회 2,553
11-03-11 조회 2,571
11-02-07 조회 2,401
11-01-18 조회 2,721
11-01-12 조회 2,442
11-01-06 조회 2,532
10-12-10 조회 2,797
10-12-09 조회 2,650
10-12-08 조회 5,080
10-11-29 조회 2,910
10-11-28 조회 2,821
10-11-28 조회 2,656
10-11-19 조회 2,846
10-11-04 조회 2,718
10-10-21 조회 2,443
10-10-19 조회 3,402
10-10-12 조회 2,729
10-10-07 조회 2,554
10-09-30 조회 3,516
10-09-17 조회 3,928
10-09-17 조회 3,872
10-05-31 조회 3,583
09-12-31 조회 3,714
09-07-23 조회 5,042
08-09-16 조회 3,245
08-09-06 조회 2,985
08-09-04 조회 3,408
08-08-16 조회 4,438
08-08-15 조회 3,307
08-08-14 조회 4,522
08-08-12 조회 3,992
08-07-23 조회 2,808
08-07-10 조회 3,758
08-07-04 조회 2,985
08-06-27 조회 7,627
08-06-19 조회 4,937
08-05-27 조회 5,540
08-05-25 조회 5,375
08-05-23 조회 3,449
08-05-23 조회 4,618
08-05-21 조회 4,968
08-05-20 조회 4,083
08-05-20 조회 3,310
08-04-18 조회 4,373