zoukankan      html  css  js  c++  java
  • 关于jquery 取值,赋值常用控件的问题

    一、文本框

    对于单个文本框:

    1.获取文本框:

    $("#id").val()

    2.赋值:

    $("#id").val("赋值");

    或 (清空即赋值为“”)

    $("#id").attr("value","赋值");

    3.清空多个文本框:

    $("input[type=text]").each(function(){
    
        $(this).val("");
    
    });

    二、下拉框(select)

    1.获取选中的:

    第一种方式

    $('#id option:selected').text();//选中的文本
    
    $('#id option:selected') .val(); 或者 $("#id").val()         //选中的值
    
    $("#id ").get(0).selectedIndex;//索引 

    第二种方式

    $("#tesetSelect").find("option:selected").text();//选中的文本
                          …….val();//选中值
                        …….get(0).selectedIndex;//索引

    2.设置选中项:

    $("#id").val("要设置为选中项的value值");  或者  
    
    $("#id option[value=要设置的value值(不加引号)]").attr("selected",true);    或
    
    $("#id").find("option[value=-1]").attr("selected",true); 

    三、radio

    1.获取选中项:

    $('input:radio:checked').val();
    
    $("input[type='radio']:checked").val();
    
    $("input[name='rd']:checked").val();

    2.设置第一个Radio为选中值:

    $('input:radio:first').attr('checked', 'checked');

    或者

    $('input:radio:first').attr('checked', true);

    3.根据id或value设置选中项:

    $("#id").attr("checked",true);
    
    $("input:radio[value=值]").attr("checked",true);

    4.清空选中项:

    $("input:radio:checked").attr("checked",false);

     

  • 相关阅读:
    dijkstra (模板)
    LUOGU P2476 [SCOI2008]着色方案
    LUOGU P2344 奶牛抗议 (树状数组优化dp)
    LaTeX 公式大全
    LUOGU P1505 [国家集训队]旅游 (树链剖分+线段树)
    LUOGU P2416 泡芙 (缩点+树剖)
    LUGOU P1092 虫食算
    BZOJ 3090: Coci2009 [podjela] (树形背包)
    bzoj 4823 [Cqoi2017]老C的方块——网络流
    bzoj 3158 千钧一发——网络流
  • 原文地址:https://www.cnblogs.com/shuilangyizu/p/8566159.html
Copyright © 2011-2022 走看看