selected 如何给tr选中_js对selected的操作

动态删除select中的所有options:

document.getElementById("ddlResourceType").options.length=0;

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

document.getElementById("ddlResourceType").options.remove(indx);

动态添加select中的项option:

document.getElementById("ddlResourceType").options.add(new Option(text,value));

上面在IE和FireFox都能测试成功,希望以后你可以用上。

其实用标准的DOM操作也可以,就是document.createElement,appendChild,removeChild之类的。

取值方面

function getvalue(obj)

{

var m=obj.options[obj.selectedIndex].value

alert(m);//获取value

var n=obj.options[obj.selectedIndex].text

alert(n);//获取文本

}

==============================================================================

1 检测是否有选中

if (objSelect.selectedIndex > - 1 ) {

// 说明选中

} else {

// 说明没有选中

}

2 删除被选中的项

objSelect.options[objSelect.selectedIndex] = null ;

3 增加项

objSelect.options[objSelect.length] = new Option( " 你好 " , " hello " );

4 修改所选择中的项

objSelect.options[objSelect.selectedIndex] = new Option( " 你好 " , " hello " );

5 得到所选择项的文本

objSelect.options[objSelect.selectedIndex].text;

6 得到所选择项的值

objSelect.options[objSelect.selectedIndex].value;

======================================================================

JS对select动态添加options操作[IE和FireFox兼容]

function xlbchange(s){

switch (s){

case "1" :

document.getElementById("lb").options.length=0;

var soojs_value=[0,1,2,3];

var soojs_text=["精神提炼","作风设计","目标设置","理念提升"];

for ( var i=0;i

var   oOption   =   document.createElement("OPTION");

oOption.value=soojs_value[i];

oOption.text=soojs_text[i];

zpmange.lb.options.add(oOption);

}

break;

case "2" :

document.getElementById("lb").options.length=0;

var soojs_value=[0,1,2,3,4,5,6,7];

var soojs_text=["校徽","校训","校歌","校名字体","校史展室","宣传画册","宣传光盘","办公用品纪念品"];

for ( var i=0;i

var   oOption   =   document.createElement("OPTION");

oOption.value=soojs_value[i];

oOption.text=soojs_text[i];

zpmange.lb.options.add(oOption);

}

break;

case "3":

document.getElementById("lb").options.length=0;

var soojs_value=[0,1,2,3];

var soojs_text=["校园景观设计","校园雕塑设计","校园浮雕设计","走廊文化设计"];

for ( var i=0;i

var   oOption   =   document.createElement("OPTION");

oOption.value=soojs_value[i];

oOption.text=soojs_text[i];

zpmange.lb.options.add(oOption);

}

break;

case "4":

document.getElementById("lb").options.length=0;

var soojs_value=[0,1,2];

var soojs_text=["学校制度","文化活动","行为规范"];

for ( var i=0;i

var   oOption   =   document.createElement("OPTION");

oOption.value=soojs_value[i];

oOption.text=soojs_text[i];

zpmange.lb.options.add(oOption);

}

break;

default :

document.getElementById("lb").options.length=0;

var   oOption   =   document.createElement("OPTION");

oOption.value=0;

oOption.text="请选择作品类别";

zpmange.lb.options.add(oOption);

}

}

动态删除select中的所有options:

document.getElementById("ddlResourceType").options.length=0;

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

document.getElementById("ddlResourceType").options.remove(indx);

动态添加select中的项option:

document.getElementById("ddlResourceType").options.add(new Option(text,value));

上面在IE和FireFox都能测试成功,希望以后你可以用上。

其实用标准的DOM操作也可以,就是document.createElement,appendChild,removeChild之类的。

取值方面

function getvalue(obj)

{

var m=obj.options[obj.selectedIndex].value

alert(m);//获取value

var n=obj.options[obj.selectedIndex].text

alert(n);//获取文本

}

例子:

请选择作品系统

理念视别系统

视觉识别系统

环境视别系统

行为视别系统

请选择作品类别

、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、

1判断select选项中 是否存在Value="paraValue"的Item

2向select选项中 加入一个Item

3从select选项中 删除一个Item

4删除select中选中的项

5修改select选项中 value="paraValue"的text为"paraText"

6设置select中text="paraText"的第一个Item为选中

7设置select中value="paraValue"的Item为选中

8得到select的当前选中项的value

9得到select的当前选中项的text

10得到select的当前选中项的Index

11清空select的项

// 1.判断select选项中 是否存在Value="paraValue"的Item

function jsSelectIsExitItem(objSelect, objItemValue) {

var isExit = false;

for (var i = 0; i < objSelect.options.length; i++) {

if (objSelect.options.value == objItemValue) {

isExit = true;

break;

}

}

return isExit;

}

// 2.向select选项中 加入一个Item

function jsAddItemToSelect(objSelect, objItemText, objItemValue) {

//判断是否存在

if (jsSelectIsExitItem(objSelect, objItemValue)) {

alert("该Item的Value值已经存在");

} else {

var varItem = new Option(objItemText, objItemValue);

objSelect.options.add(varItem);

alert("成功加入");

}

}

// 3.从select选项中 删除一个Item

function jsRemoveItemFromSelect(objSelect, objItemValue) {

//判断是否存在

if (jsSelectIsExitItem(objSelect, objItemValue)) {

for (var i = 0; i < objSelect.options.length; i++) {

if (objSelect.options.value == objItemValue) {

objSelect.options.remove(i);

break;

}

}

alert("成功删除");

} else {

alert("该select中 不存在该项");

}

}

// 4.删除select中选中的项

function jsRemoveSelectedItemFromSelect(objSelect) {

var length = objSelect.options.length - 1;

for(var i = length; i >= 0; i--){

if(objSelect.selected == true){

objSelect.options = null;

}

}

}

// 5.修改select选项中 value="paraValue"的text为"paraText"

function jsUpdateItemToSelect(objSelect, objItemText, objItemValue) {

//判断是否存在

if (jsSelectIsExitItem(objSelect, objItemValue)) {

for (var i = 0; i < objSelect.options.length; i++) {

if (objSelect.options.value == objItemValue) {

objSelect.options.text = objItemText;

break;

}

}

alert("成功修改");

} else {

alert("该select中 不存在该项");

}

}

// 6.设置select中text="paraText"的第一个Item为选中

function jsSelectItemByValue(objSelect, objItemText) {

//判断是否存在

var isExit = false;

for (var i = 0; i < objSelect.options.length; i++) {

if (objSelect.options.text == objItemText) {

objSelect.options.selected = true;

isExit = true;

break;

}

}

//Show出结果

if (isExit) {

alert("成功选中");

} else {

alert("该select中 不存在该项");

}

}

// 7.设置select中value="paraValue"的Item为选中

document.all.objSelect.value = objItemValue;

// 8.得到select的当前选中项的value

var currSelectValue = document.all.objSelect.value;

// 9.得到select的当前选中项的text

var currSelectText = document.all.objSelect.options[document.all.objSelect.selectedIndex].text;

// 10.得到select的当前选中项的Index

var currSelectIndex = document.all.objSelect.selectedIndex;

// 11.清空select的项

document.all.objSelect.options.length = 0;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值