zoukankan      html  css  js  c++  java
  • jquery select取值,赋值操作

    [导读]  jquery select取值,赋值操作一、获取Select获取select 选中的 text : $(" ddlRegType") find("option:selected") text();获取select选中的索引: $(" ddlRegType") get(0) selectedIndex;二、设置Select设

    select" rel="nofollow">jquery select取值,赋值操作

    一、获取Select 

    获取select 选中的 text : 

    $("#ddlRegType").find("option:selected").text();

     

    获取select选中的索引: 

    $("#ddlRegType").get(0).selectedIndex;

     

    二、设置Select

    设置select 选中的索引: 

    $("#ddlRegType").get(0).selectedIndex = index;//index为索引值

     

    设置select 选中的value:

    $("#ddlRegType").attr("value","Normal“);

    $("#ddlRegType").val("Normal");

    $("#ddlRegType").get(0).value = value;

     

    设置select 选中的text:

     1 var count = $("#ddlRegType option").length;
     2 
     3 for(var i=0;i<count;i++)  
     4 {

     5   if($("#ddlRegType ").get(0).options[i].text == text)  
     6     {  
     7         $("#ddlRegType ").get(0).options[i].selected = true;  
     8         break;  
     9     }  
    10 }

     

    $("#select_id option[text='jQuery']").attr("selected", true);

     

    设置select option项:

     $("#select_id").append("<option value='Value'>Text</option>");  //添加一项option

     $("#select_id").prepend("<option value='0'>请选择</option>"); //在前面插入一项option

     $("#select_id option:last").remove(); //删除索引值最大的Option

     $("#select_id option[index='0']").remove();//删除索引值为0的Option

     $("#select_id option[value='3']").remove(); //删除值为3的Option

     $("#select_id option[text='4']").remove(); //删除TEXT值为4的Option

     

    清空 Select:

    $("#ddlRegType ").empty();


    下拉框:

    var cc1   = $(".formc select[@name='country'] option[@selected]").text(); //得到下拉菜单的选中项的文本(注意中间有空格)
    var cc2 = $('.formc select[@name="country"]').val();   //得到下拉菜单的选中项的值
    var cc3 = $('.formc select[@name="country"]').attr("id"); //得到下拉菜单的选中项的ID属性值
    $("#select").empty();//清空下拉框//$("#select").html('');
    $("<option value='1'>1111</option>").appendTo("#select")//添加下拉框的option

    稍微解释一下:
    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(){ //由于复选框一般选中的是多个,所以可以循环输出
       alert($(this).val());
       });

    $("#chk1").attr("checked",'');//不打勾
    $("#chk2").attr("checked",true);//打勾
    if($("#chk1").attr('checked')==undefined){} //判断是否已经打勾

    //遍历option和添加、移除option
    function changeShipMethod(shipping){
     var len = $("select[@name=ISHIPTYPE] option").length
     if(shipping.value != "CA"){
      $("select[@name=ISHIPTYPE] option").each(function(){
       if($(this).val() == 111){
        $(this).remove();
       }
      });
     }else{
      $("<option value='111'>UPS Ground</option>").appendTo($("select[@name=ISHIPTYPE]"));
     }
    }


    //取得下拉选单的选取值

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

  • 相关阅读:
    懂一点Python系列——快速入门
    DatagridView 控件列顺序与设置的不一样
    WinForm Column cannot be added because its CellType property is null.
    Rabbitmq消息服务器通讯异常: name must not be blank
    Redis 数据库
    redis 安装与使用
    windows 服务的安装与卸载之bat脚本命令
    以超级管理员方式运行bat文件
    CMD 下切换目录
    C#导出Excel后关闭进程EXCEL.EXE
  • 原文地址:https://www.cnblogs.com/wybshyy/p/13783771.html
Copyright © 2011-2022 走看看