JQuery動作表單相關使用總結

來源:互聯網
上載者:User

標籤:http   java   使用   io   art   for   

select下拉式清單onChange事件之JQuery實現:

Java代碼  
  1. JQuery:  
  2. $(document).ready(function () {       
  3.        $("#selectMenu").bind("change", function () {   
  4.         if ($(this).val() == "pro1") {   
  5.             $("#pro1").slideDown();   
  6.             $("#pro2").slideUp();   
  7.         }   
  8.         else if($(this).val() =="pro2") {   
  9.             $("#pro2").slideDown();   
  10.             $("#pro1").slideUp();   
  11.         }   
  12.     });   
  13. });   
  14.   
  15. HTML:   
  16. <select id="selectMenu">    
  17.     <option value="" >Please select product below</option>    
  18.     <option value="pro1">Product 1</option>    
  19.     <option value="pro2">Product 2</option>    
  20. </select>    

 

Java代碼  
  1. //1.jQuery對select的基本操作  
  2. $("#select_id").change(function(){ //code...});   //為Select添加事件,當選擇其中一項時觸發  
  3.   
  4. var checkValue=$("#select_id").val();  //擷取Select選擇的Value  
  5. var checkValue = $(‘.formc select[@name="country"]‘).val(); //得到下拉式功能表name=country的選中項的值  
  6. var checkValue=$("#select_id").val().join(","); //擷取select多選(multiple="true"時候) 的value  
  7.   
  8. var checkText = $("#select_id").find("option:selected").text();  //擷取Select選擇的Text  
  9. var checkText = $("select[@name=country] option[@selected]").text();  //擷取select被選中項的文本(注意中間有空格)  
  10. var checkText = $("#select_id option:selected").text();  
  11.   
  12. var cc2 = $(‘.formc select[@name="country"]‘).val(); //得到下拉式功能表的選中項的值  
  13. var cc3 = $(‘.formc select[@name="country"]‘).attr("id"); //得到下拉式功能表的選中項的ID屬性值  
  14.   
  15.   
  16. var checkIndex=$("#select_id ").get(0).selectedIndex;  //擷取Select選擇的索引值  
  17. var maxIndex=$("#select_id option:last").attr("index");  //擷取Select最大的索引值  
  18.   
  19. $("#select_id ").get(0).selectedIndex = 1;  //設定Select索引值為1(第二項)的項選中  
  20. $(‘#select_id‘)[0].selectedIndex = 1;       //設定Select索引值為1(第二項)的項選中  
  21. $("#select_id ").val(4);                    //設定Select的Value值為4的項選中  
  22. $("#select_id option[text=‘jQuery‘]").attr("selected", true);   //設定Select的Text值為jQuery的項選中  
  23. $("#select_id").attr("value",‘-sel3‘);      //設定value=-sel3的項目為當前選中項  
  24.   
  25. $("#select_id").empty();    //清空下拉框  
  26.   
  27. $("#select_id").append("<option value=‘Value‘>Text</option>");  //為Select追加一個Option(下拉項)  
  28. $("<option value=‘1‘>1111</option><option value=‘2‘>2222</option>").appendTo("#select_id")//添加下拉框的option  
  29. $("#select_id").prepend("<option value=‘0‘>請選擇</option>");  //為Select插入一個Option(第一個位置)  
  30. $("#select_id option:last").remove();       //刪除Select中索引值最大Option(最後一個)  
  31. $("#select_id option[index=‘0‘]").remove(); //刪除Select中索引值為0的Option(第一個)  
  32. $("#select_id option[value=‘3‘]").remove(); //刪除Select中Value=‘3‘的Option  
  33. $("#select_id option[text=‘4‘]").remove();  //刪除Select中Text=‘4‘的Option  
  34.   
  35.   
  36. //2.jquery對radio的基本操作  
  37. var item = $(‘input[@name=items][@checked]‘).val();  //擷取一組radio被選中項的值  
  38. var rval = $("input[@type=radio][@checked]").val();   //得到單選框的選中項的值(注意中間沒有空格)  
  39. $(‘input[@name=items]‘).get(1).checked = true;  //radio單選組的第二個元素為當前選中值  
  40. $("input[@type=radio]").attr("checked",‘2‘);     //設定value=2的項目為當前選中項  
  41. $("input[@type=radio][@value=2]").attr("checked",‘checked‘); //設定單選框value=2的為選中狀態.(注意中間沒有空格)  
  42.   
  43.   
  44. //3.jquery對checkbox的基本操作  
  45. $("#checkbox_id").attr("value"); //多選框checkbox  
  46. $("input[@type=checkbox][@checked]").val(); //得到複選框的選中的第一項的值  
  47. $("input[@type=checkbox][@checked]").each(function(){ //由於複選框一般選中的是多個,所以可以迴圈輸出  
  48.     alert($(this).val());  
  49. });  
  50. $("#chk1").attr("checked",‘‘);//不打勾  
  51. $("#chk2").attr("checked",true);//打勾  
  52. if($("#chk1").attr(‘checked‘)==undefined) //判斷是否已經打勾  
  53.   
  54.   
  55. //4.jquery對text的基本操作  
  56. $("#txt").attr("value");    //文字框,文本地區:  
  57. $("#txt").attr("value",‘‘); //清空內容  
  58. $("#txt").attr("value",‘11‘);//填充內容  

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.