07 พฤษภาคม 2552

How to get and set form element values

Getting or setting the values of form elements can be a challenging chore with traditional JavaScript. We use different methods to find or set the value of a text box, radio button, or select list. jQuery provides the val() method to make this easier for us. Using val() without a parameter will get the value of any referenced form element. Giving the method a parameter will set the form element to the value indicated by the parameter.

//getting values
alert( $("input#mytextbox").val() );
alert( $("select#mylist").val() );
alert( $("input#myradio").val() );
//
//setting values
$("input#mytextbox").val("New text value");
$("select#mylist").val("ItemOne");
$("input#myradio").val(3);

ที่มา http://jquery.open2space.com/node/14

ไม่มีความคิดเห็น: