javascript jquery對form元素的常見操作詳解,jqueryform

來源:互聯網
上載者:User

javascript jquery對form元素的常見操作詳解,jqueryform

1.下拉框 select :

移除option

$("#ID option").each(function(){   if($(this).val() == 111){   $(this).remove(); }});

添加option

$("<option value='111'>UPS Ground</option>").appendTo($("#ID"));

取得下拉選單的選取值

//取下拉選中的文本$('#testSelect option:selected').text();$("#testSelect").find('option:selected').text();document.all.objSelect.options[document.all.objSelect.selectedIndex].text; //js操作 objSelect為select的name//取得下拉選中值$("#testSelect").val();//js操作document.getElementById('objSelect').value=2;document.all.objSelect.value;

根據option的值選中下拉框

$('#testSelect').val('111');document.all.objSelect.value = objItemValue; //js操作 objSelect為select的name或者iddocument.getElementById('sel').value=objItemValue;

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

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

2,單選框 radio :

$("input[type=radio][checked]").val(); //得到單選框的選中項的值(注意中間沒有空格)$(':radio[name="radio"]:checked').val();//第二種方法$("input[type=radio][value=2]").attr("checked",'checked'); //設定單選框value=2的為選中狀態.(注意中間沒有空格)$(':radio[value="2"]').attr('checked','checked');

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

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

3,複選框 checkbox :(其它寫法參見上面:radio)

$("input[type='checkbox'][checked]").val(); //得到複選框的選中的第一項的值$("#chk1").attr("checked",'');//不打勾$("#chk2").attr("checked",true);// 打勾//全選/不選 $("#selectAll").bind('click',function(){   var sa = $(this).attr("checked");   $("input[name='sel[]']").each(function(){     this.checked=sa;   }); });

4,輸入框 input :

$(':input[name="keyword"]').val();//根據name值取得值

5,文字框 textarea :

固定文字框大小:<textarea name="123" style="resize:none;"></textarea>

以上這篇javascript jquery對form元素的常見操作詳解就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支援幫客之家。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.