jquery對表單元素的取值和賦值

2021-06-22 13:45:53 字數 1372 閱讀 5164

/*獲得text的值*/

var textval =$("#text_id").attr("value");

//或者

var textval =$("#text_id").val();

/*獲取單選按鈕的值*/

var valraio =$("input[type=raio]:checked").val();

/*獲取一組名為items的radio被選中項的值*/

var item =$('input[name=items]:checked').val();

/*獲取核取方塊的值*/

var checkboxval =$("#checkbox_id").attr("value");

/*獲取下拉列表的值*/

var selectval =$("#select_id").val();

/*文字框,文字區域*/

$("#text_id").attr("value",'');//清空內容

$("#text_id").attr("value",'test');//填充內容

/*多選框checkbox*/

$("#chk_id").attr("checked",'');//使其未勾選

$("#chk_id").attr("checked",true);//勾選

if($("#chk_id").attr('checked')==true)//判斷是否已經選中

/*單選組radio*/

$("input[type=radio]").attr("checked",'2')//設定value=2的專案為當前選中項

/*下拉框select*/

$("select_id").attr("value",'test');//設定value=test的專案為當前選中項

$("#select_id").empty();//清空下拉框

/*獲取一組名為items的radio被選中項的值*/

var item =$('input[name=items]:checked').val();//若未被選中 則val()=undefined

/*獲取select被選中項的文字*/

var item =$("select[name=items] option:selected").text();

/*select下拉框的第二個元素為當前選中值*/

$('#select_id')[0].selectedindex=1;

/*radio單選組的第二個元素為當前選中值*/

$('input[name=items]').get(1).checked=true;

/*重置表單*/

$("form").each(function());

jquery對表單元素的取值和賦值

一 獲得text的值 txtval type text value longzhoufeng 二 獲取單選按鈕的值 三 獲取一組名為items的radio被選中項的值 四 獲取核取方塊的值 水果 chkbox name fruit type checkbox value 水果 五 獲取下拉列表的值 ...

jQuery對表單元素的取值和賦值操作

好久沒用過jquery了,很多東西都陌生了,還是應該多練啊。jquery讀取input元素的值 使用者名稱 username val zhangsan 密碼 以前老是習慣性的這樣取值 var username username value 這樣是取不到值的,因為此時 username 已經不是ele...

jQuery對表單元素的取值和賦值操作

好久沒用過jquery了,很多東西都陌生了,還是應該多練啊。jquery讀取input元素的值 使用者名稱 username val zhangsan 密碼 以前老是習慣性的這樣取值 var username username value 這樣是取不到值的,因為此時 username 已經不是ele...