Heim  >  Artikel  >  Web-Frontend  >  javascript options属性集合操作代码_表单特效

javascript options属性集合操作代码_表单特效

WBOY
WBOYOriginal
2016-05-16 18:38:041170Durchsuche
复制代码 代码如下:





用下面的代码可以访问到下拉框中选项:
复制代码 代码如下:

// 得到选项对象
document.forms['testform'].testselect.options[i]

如果你想删除option
复制代码 代码如下:

document.forms['testform'].testselect.options[i] = null;

把这个选项对象标志为null,这个选项就完全从列表中删除了。
注意:这个操作会影响option的数量。假设在上面的实例中,你删除了option[1] ,原来的option[2] 元素('Third option')会变成option[1] 元素(option元素按照先后顺序顶上去)。
创建一个新的option,如下:
复制代码 代码如下:

document.forms['testform'].testselect.options[i] = new Option('new text','new value');

用户在页面中看到option显示的文本和value值是这个option的VALUE 属性。
当表单提交时,VALUE 值传递到WEB服务器。
如果想要全部清空 select box 中的options, 如下:
复制代码 代码如下:

document.forms['testform'].testselect.option.length = 0;
Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn