jquery基本表單驗證

2021-09-01 04:21:44 字數 4229 閱讀 4823

jquery獲取select選擇的text和value:

語法解釋:

1. $("#select_id").change(function());   //為select新增事件,當選擇其中一項時觸發

2. var checktext=$("#select_id").find("option:selected").text();  //獲取select選擇的text

3. var checkvalue=$("#select_id").val();  //獲取select選擇的value

4. var checkindex=$("#select_id ").get(0).selectedindex;  //獲取select選擇的索引值

5. var maxindex=$("#select_id option:last").attr("index");  //獲取select最大的索引值

jquery設定select選擇的 text和value:

語法解釋:

1. $("#select_id ").get(0).selectedindex=1;  //設定select索引值為1的項選中

2. $("#select_id ").val(4);   // 設定select的value值為4的項選中

3. $("#select_id option[text='jquery']").attr("selected", true);   //設定select的text值為jquery的項選中

jquery新增/刪除select的option項:

語法解釋:

//為select追加乙個option(下拉項)

2. $("#select_id").prepend("請選擇");  

//為select插入乙個option(第乙個位置)

3. $("#select_id option:last").remove();  

//刪除select中索引值最大option(最後乙個)

4. $("#select_id option[index='0']").remove();  

//刪除select中索引值為0的option(第乙個)

5. $("#select_id option[value='3']").remove();  

//刪除select中value='3'的option

5. $("#select_id option[text='4']").remove();  

//刪除select中text='4'的option

jquery radio取值,checkbox取值,select取值,radio選中,checkbox選中,select選中,及其相關

獲 取一組radio被選中項的值

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

獲 取select被選中項的文字

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

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

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

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

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

獲取值:

文字框,文字區域:$("#txt").attr("value");

多選框 checkbox:$("#checkbox_id").attr("value");

單選組radio:   $("input[@type=radio][@checked]").val();

下拉框select: $('#sel').val();

控制表單元素:

文字框,文字區域:$("#txt").attr("value",'');//清空內容

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

多選框checkbox: $("#chk1").attr("checked",'');//不打勾

$("#chk2").attr("checked",true);//打勾

if($("#chk1").attr('checked')==undefined) //判斷是否已經打勾

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

下拉框 select:   $("#sel").attr("value",'-sel3');//設定value=-sel3的專案為當前選中項

$("1111

//取得下拉列表的選取值

$(#testselect option:selected').text();

或$("#testselect").find('option:selected').text();

或$("#testselect").val();

//記 性不好的可以收藏下:

1,下拉框:

稍微解釋一下:

1.select[@name='country'] option[@selected] 表示具有name 屬性,

並 且該屬性值為'country' 的select元素 裡面的具有selected 屬性的option 元素;

可以看出有@開頭的就表示後面跟 的是屬性。

2,單選框:

$("input[@type=radio][@checked]").val(); //得到單選框的 選中項的值(注意中間沒有空格)

$("input[@type=radio][@value=2]").attr("checked",'checked'); //設定單選框value=2的為選中狀態.(注意中間沒有空格)

3,核取方塊:

$("input[@type=checkbox][@checked]").val(); //得到核取方塊的選中的第一項的值

$("input[@type=checkbox][@checked]").each(function() );

$("#chk1").attr("checked",'');//不打勾

$("#chk2").attr("checked",true);// 打勾

if($("#chk1").attr('checked')==undefined){} //判斷是否已經打勾

當然jquery的選擇器是強大的. 還有很多方法.

aaass

1122

3344

5566

獲取一組radio被選中項的值

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

獲取select被選中項的文字

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

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

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

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

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

獲取值:

文字框,文字區域:$("#txt").attr("value");

多選框 checkbox:$("#checkbox_id").attr("value");

單選組radio: $("input[@type=radio][@checked]").val();

下拉框select: $('#sel').val();

控 製表單元素:

文字框,文字區域:$("#txt").attr("value",'');//清空內容

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

多選框checkbox: $("#chk1").attr("checked",'');//不打勾

$("#chk2").attr("checked",true);// 打勾

if($("#chk1").attr('checked')==undefined) //判斷是否已經打勾

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

下拉框 select: $("#sel").attr("value",'-sel3');//設定value=-sel3的專案為當前選中項

$("1111

JQuery表單驗證

舉個例子,有這麼乙個表單 在這個表單中,有名 姓 使用者名稱 密碼 確認密碼和email。他們都為非空,並且電子郵件需要是格式正確的位址 確認密碼和密碼一致。使用jquery驗證最簡單的方式是引入jquery.js和jquery validation.js兩個js檔案。然後分別在input中加入cl...

jquery表單驗證

手機號碼驗證 中文字兩個位元組 jquery.validator.addmethod byterangelength function value,element,param return this.optional element length param 0 length param 1 請確保...

jquery表單驗證

順便吐槽星期六前端群裡的出來面基了,宅毛線,出來 嗨 表單驗證基於jquery beat1.0 相容ie6 author jounreyyao data 2015 07 02 function else if u4e00 u9fa5 i.test name else datayear functio...