JS动态更改select的值和项

来源:互联网 发布:sql 使用别名 编辑:程序博客网 时间:2024/06/02 06:01

原理很简单,遍历select下面options的值,然后跟你el表达式里的值(或者其他什么值)进行对比,如果一致则设置这个option的selected = true即可。

代码如下:

function init(){var selectValue;var t;selectValue = "${busiType }";t = document.getElementById("busiType");for(i = 0; i < t.options.length; i++){if(selectValue == t.options[i].value){t.options[i].selected = true;}}}

//或者用这个更简单的$("#select").find("option[value=" + current + "]").attr("selected",true);




顺便把checkbox怎么赋值也跟出来,代码如下:

selectValue = "${canValue}";t = document.getElementById("checkbox");if(selectValue == "选中值")t.checked = true;


还有一些select的其他操作:

动态删除select中的所有options:

function deleteAllOptions(sel){sel.options.length=0; }

动态删除select中的某一项option:

function deleteOption(sel,indx){sel.options.remove(indx); }
动态添加select中的项option:

function addOption(sel,text,value){sel.options.add(new Option(text,value)); }

0 0
原创粉丝点击