javascript jquery對form元素的常見操作

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或者id
document.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>
posted @ 2012-07-03 21:06  Vhelin  阅读(712)  评论(0编辑  收藏  举报