리자

How to get and set form element values with jQuery

· 17년 전 · 2788

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에서 이동 됨]
|
댓글을 작성하시려면 로그인이 필요합니다.

프로그램

+
제목 글쓴이 날짜 조회
17년 전 조회 3,350
17년 전 조회 2,272
17년 전 조회 1,855
17년 전 조회 1,632
17년 전 조회 2,157
17년 전 조회 4,838
17년 전 조회 1,557
17년 전 조회 2,298
17년 전 조회 2,271
17년 전 조회 2,542
17년 전 조회 2,251
17년 전 조회 4,577
17년 전 조회 3,074
17년 전 조회 2,985
17년 전 조회 1,707
17년 전 조회 1,350
17년 전 조회 4,093
17년 전 조회 1,757
17년 전 조회 1,752
17년 전 조회 2,238
17년 전 조회 1,990
17년 전 조회 1,641
17년 전 조회 4,056
17년 전 조회 1,878
17년 전 조회 3,232
17년 전 조회 3,140
17년 전 조회 1,139
17년 전 조회 1,972
17년 전 조회 1,715
17년 전 조회 1,964
17년 전 조회 2,778
17년 전 조회 3,145
17년 전 조회 3,350
17년 전 조회 3,458
17년 전 조회 1,568
17년 전 조회 1,546
17년 전 조회 2,362
17년 전 조회 2,101
17년 전 조회 2,400
17년 전 조회 2,929
17년 전 조회 3,403
17년 전 조회 2,491
17년 전 조회 1,768
17년 전 조회 3,338
17년 전 조회 3,196
17년 전 조회 3,175
17년 전 조회 4,046
17년 전 조회 2,691
17년 전 조회 2,530
17년 전 조회 2,789
17년 전 조회 3,027
17년 전 조회 2,741
17년 전 조회 1,571
17년 전 조회 1,998
17년 전 조회 1,599
17년 전 조회 2,041
17년 전 조회 2,641
17년 전 조회 8,812
17년 전 조회 3,273
17년 전 조회 4,350
17년 전 조회 2,105
17년 전 조회 3,778
17년 전 조회 1,684
17년 전 조회 1,495
17년 전 조회 2,431
17년 전 조회 1,426
17년 전 조회 1,741
17년 전 조회 1,587
17년 전 조회 2,652
17년 전 조회 1,609
17년 전 조회 1,206
17년 전 조회 1,345
17년 전 조회 3,226
17년 전 조회 2,269
17년 전 조회 2,075
17년 전 조회 1,324
17년 전 조회 2,548
17년 전 조회 1,277
17년 전 조회 1,246
17년 전 조회 1,491
17년 전 조회 2,858
17년 전 조회 2,513
17년 전 조회 2,587
17년 전 조회 1,290
17년 전 조회 1,405
17년 전 조회 3,372
17년 전 조회 2,941
17년 전 조회 4,214
17년 전 조회 2,384
17년 전 조회 2,479
17년 전 조회 1,537
17년 전 조회 2,645
17년 전 조회 2,325
17년 전 조회 3,090
17년 전 조회 2,588
17년 전 조회 1,747
17년 전 조회 2,534
17년 전 조회 1,690
17년 전 조회 1,276
17년 전 조회 2,346