• 如何用原生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);
  • 相关阅读:
    PHP中读写文件
    HTMLmeta
    穷举密码序列(C#)
    多线程访问winform控件示例
    Web服务器
    [转]javascript 之 select下拉框
    大中小字体显示
    WPF中StringFormat的用法
    常用的 19 条 MySQL 优化
    resin部署安装
  • 原文地址:https://www.cnblogs.com/imustun/p/10067778.html
Copyright © 2020-2023  润新知