更改javascript中下拉列表的值

时间:2011-08-08 22:50:04

标签: javascript drop-down-menu

我有一个表单,其中有16个用户必须填写的单个元素。在这些元素的下拉列表中,值为0-16。所有这些元素的初始值为零。

用户可以为每个元素选择1到16的每个值,但是他们只能选择每个元素 元素一次。当他们选择每个值时,我正在更改所有元素的下拉值以消除所选值。最后,每个下拉列表中剩下的唯一值是0以及用户为该下拉列表选择的值。如果用户将值更改回零,它还会返回值。

我设置它的方式在Chrome和FireFox中运行良好,但在IE中失败。问题是opts.options是一个未定义的错误。不知何故,它适用于所有浏览器,但IE。我想我需要将元素作为对象传递给填充下拉列表的方法。

以下是在PHP / HTML中调用方法的方法,它也调用了 加载方法来初始化下拉列表:

echo "<TD bgcolor=$color width=15><SELECT name='$pts' onChange='populatePoints(this)' </SELECT></TD>n";

以下是相关的JS:

   function populatePoints(x){

 setOptions (document.form1.G1_Points)

 setOptions (document.form1.G2_Points)

 setOptions (document.form1.G3_Points)

 setOptions (document.form1.G4_Points)

 setOptions (document.form1.G5_Points)

 setOptions (document.form1.G6_Points)

 setOptions (document.form1.G7_Points)

 setOptions (document.form1.G8_Points)

 setOptions (document.form1.G9_Points)

 setOptions (document.form1.G10_Points)

 setOptions (document.form1.G11_Points)

 setOptions (document.form1.G12_Points)

 setOptions (document.form1.G13_Points)

 setOptions (document.form1.G14_Points)

 setOptions (document.form1.G15_Points)

 setOptions (document.form1.G16_Points)


 }

 function setOptions (opts){

 pointValue = opts.value

 opts.options.length = 0

 var x = 0

 opts.options[x] = new Option(0)

 x++

 for (i=1;i<17;i++) {

 if (document.form1.G1_Points.value != i &&

 document.form1.G2_Points.value != i &&

 document.form1.G3_Points.value != i &&

 document.form1.G4_Points.value != i &&

 document.form1.G5_Points.value != i &&

 document.form1.G6_Points.value != i &&

 document.form1.G7_Points.value != i &&

 document.form1.G8_Points.value != i &&

 document.form1.G9_Points.value != i &&

 document.form1.G10_Points.value != i &&

 document.form1.G11_Points.value != i &&

 document.form1.G12_Points.value != i &&

 document.form1.G13_Points.value != i &&

 document.form1.G14_Points.value != i &&

 document.form1.G16_Points.value != i &&

 document.form1.G15_Points.value != i){

 opts.options[x] = new Option(i)

 x++}}

 opts.value = pointValue

 }

1 个答案:

答案 0 :(得分:0)

IE不喜欢以这种方式清算选项:opts.options.length = 0

在IE中清除您的选项:

for (i=options.length-1; i>=0; i--)   {
      select.removeChild(options[i]);   
}

或这样做(最快的清除方式):

select.innerHTML = "";