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);

  • 相关阅读:
    [Luogu1126] 机器人搬重物
    [POJ1830] 开关问题
    [bzoj3139] 比赛
    [POJ3349] Snowflake Snow Snowflakes
    The Tour
    [POJ3740] Easy Finding
    [vijos1453] 曼哈顿距离
    [POJ1632] Vase collection
    Codewars笔记
    Appium知识积累
  • 原文地址:https://www.cnblogs.com/lpynb/p/5474913.html
Copyright © 2011-2022 走看看