zoukankan      html  css  js  c++  java
  • 【转】jQuery获取客户端控件select

    原文:http://ghl68754031.k15.wg8.com/

    jQuery获取客户端控件select

    一、 获取select中选择的text与value相关的值

    获取select选择的Text : var checkText=$("#slc1").find("option:selected").text();

    获取select选择的value:var checkValue=$("#slc1").val();

    获取select选择的索引值: var checkIndex=$("#slc1 ").get(0).selectedIndex;

    获取select最大的索引值: var maxIndex=$("#slc1 option:last").attr("index");

    二、  设置select选择的Text和Value

    设置select索引值为1的项选中:$("#slc1 ").get(0).selectedIndex=1;  

    设置select的value值为4的项选中: $("#slc1 ").val(4);  

    设置select的Text值为JQuery的选中:

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

    PS:特别要注意一下第三项的使用哦。看看JQuery的选择器功能是如此地强大呀!

    三、 添加删除option项

    为select追加一个Option(下拉项)

    $("#slc2").append("<option value='"+i+"'>"+i+"</option>");

    为select插入一个option(第一个位置)

    $("#slc2").prepend("<option value='0'>请选择</option>");

    PS: prepend 这是向所有匹配元素内部的开始处插入内容的最佳方式。

    删除select中索引值最大option(最后一个)

    $("#slc2 option:last").remove();

    删除select中索引值为0的option(第一个)

    $("#slc2 option[index='0']").remove();

    删除select中value=’3’的option

    $("#slc2 option[value='3']").remove();

    删除select中text=’4’的option

    $("#slc2 option[text='3']").remove();

  • 相关阅读:
    JS Dom_API
    JS 动态表格(添加、删除行)
    将本地网页上传到 apache2 及 github 的步骤
    软件工程之美 第一周
    树莓派安装芯片驱动并测试
    Visoul Studio 2019 远程调试 中文乱码
    Visoul Studio 2019 远程调试 RaspberryPi C 项目
    课设提纲
    PHP PDO 一 : 常用方法
    设置子域名及申请其证书
  • 原文地址:https://www.cnblogs.com/luofuxian/p/2377448.html
Copyright © 2011-2022 走看看