zoukankan      html  css  js  c++  java
  • jQuery操作Select

    jQuery是如何控制和操作select的。先看下面的html代码

    <select id="test">
    <option value="1">选项一<option>
    <option value="2">选项一<option>
    ...
    <option value="n">选项N<option>
    </select>

    所谓jQuery操作“select”, 说的更确切一些是应该是jQuery控制 “option”, 看下面的jQuery代码:


    //获取第一个option的值
    $('#test option:first').val();

    //最后一个option的值
    $('#test option:last').val();

    //获取第二个option的值
    $('#test option:eq(1)').val();

    //获取选中的值
    $('#test').val();
    $('#test option:selected').val();

    //设置值为2的option为选中状态
    $('#test').attr('value','2');

    //设置最后一个option为选中
    $('#test option:last').attr('selected','selected');
    $("#test").attr('value' , $('#test option:last').val());
    $("#test").attr('value' , $('#test option').eq($('#test option').length - 1).val());

    //获取select的长度
    $('#test option').length;

    //添加一个option
    $("#test").append("<option value='n+1'>第N+1项</option>");
    $("<option value='n+1'>第N+1项</option>").appendTo("#test");

    //添除选中项
    $('#test option:selected').remove();

    //删除项选中(这里删除第一项)
    $('#test option:first').remove();、

    //指定值被删除
    $('#test option').each(function(){
    if( $(this).val() == '5'){
    $(this).remove();
    }
    });
    $('#test option[value=5]').remove();

    //获取第一个Group的标签
    $('#test optgroup:eq(0)').attr('label');

    //获取第二group下面第一个option的值
    $('#test optgroup:eq(1) : option:eq(0)').val();
  • 相关阅读:
    OSCP Learning Notes
    OSCP Learning Notes
    OSCP Learning Notes
    OSCP Learning Notes
    OSCP Learning Notes
    OSCP Learning Notes
    OSCP Learning Notes
    OSCP Learning Notes
    Codeforces Round #601 (Div. 2) E2 Send Boxes to Alice (Hard Version)
    Codeforces Round #601 (Div. 2) E1 Send Boxes to Alice (Easy Version)
  • 原文地址:https://www.cnblogs.com/Don/p/2541966.html
Copyright © 2011-2022 走看看