zoukankan      html  css  js  c++  java
  • 如何用原生js或jquery设置select的值

    1、原生js设置select值的方法

    (1)有时可能需要隐藏select,但是还得需要更改select所传递的值。(select的默认选中之为第一个,即下标为0的选项值

    var gd2=document.getElementById("goods_name2");
    //为防止有时指定id元素不存在导致的异常
    if(gd2)
    { gd2[
    0].value=newvalue; }

     (2)原生js更改select选定的值

    var gd2=document.getElementById("goods_name2");
    //为防止有时指定id元素不存在导致的异常
    if(gd2)
    { 
       gd2.value=newvalue; //更改选定项,值为select选项中原有值即换一个选择项)
    }
    var obj = document.getElementById("goods_name"); //定位id
    var index = obj.selectedIndex; // 选中索引
    obj.options[index].value=newvalue;//更改选定项的原有值(产生了新的选择项

    2、jquery设置select值的方法

    //设置select选定的值
    $('#goods_name2 option:selected') .val(newvalue);
  • 相关阅读:
    CodeForces 450
    CodeForces 400
    CodeForces 1
    [HDU POJ] 逆序数
    [HDU 1166] 敌兵布阵
    [转] 树状数组学习
    关于1月4日到1月7日
    [HDU 1565+1569] 方格取数
    [POJ 1459] Power Network
    [转] 网络流算法--Ford-Fulkerson方法及其多种实现
  • 原文地址:https://www.cnblogs.com/imustun/p/10067778.html
Copyright © 2011-2022 走看看