zoukankan      html  css  js  c++  java
  • 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>
  • 相关阅读:
    正则式记录
    限制键盘只能按数字键、小键盘数字键、退格键
    windows服务安装记录
    CheckBox使用记录
    you need to be root to perform this command
    Code First 更新数据库 记录
    EF查询记录
    sqlserver数据库存储汉字出现?
    【转】THE ROAD TO SUCCESS--听ERIC XING讲课记录
    Nice Computer Vision package collections
  • 原文地址:https://www.cnblogs.com/helin/p/2575330.html
Copyright © 2011-2022 走看看