简体   繁体   English

更改JavaScript中的下拉菜单的值

[英]Change value of dropdowns in javascript

I have a form where there are 16 individual elements the user must fill. 我有一个表格,其中用户必须填写16个单独的元素。 In the dropdown for these elements are the values 0-16. 这些元素的下拉列表中的值为0-16。 The initial value of all these elements is zero. 所有这些元素的初始值为零。

The user may choose each value from 1 to 16 for each element, but they may only choose each element once. 用户可以为每个元素从1到16中选择每个值,但是他们只能为每个元素选择一次。 As they choose each value, I am changing the value of the dropdowns of all the elements to eliminate the chosen values. 当他们选择每个值时,我正在更改所有元素的下拉列表的值以消除选择的值。 In the end, the only values left in each dropdown are 0 and the value the user chose for that dropdown. 最后,每个下拉列表中剩下的唯一值为0,并且用户为该下拉列表选择了该值。 It also puts back the values if the user changes a value back to zero. 如果用户将值改回零,它也会放回值。

The way I have it set up works fine in Chrome and FireFox, but fails in IE. 我进行设置的方式在Chrome和FireFox中可以正常运行,但在IE中失败。 The problem is the opts.options is an undefined error. 问题是opts.options是未定义的错误。 Somehow it works in all browsers but IE. 它以某种方式可以在除IE之外的所有浏览器中工作。 I think I need to pass the element as an object to the method that populates the dropdowns. 我想我需要将元素作为对象传递给填充下拉菜单的方法。

Here is how the methods are called in the PHP/HTML, it also calls the methods on load to initialize the dropdowns: 这是在PHP / HTML中调用方法的方式,它还会在加载时调用方法以初始化下拉列表:

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

Here is the relevant JS: 这是相关的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

 }

IE doesn't like clearing options this way: opts.options.length = 0 IE不喜欢这样清除选项: opts.options.length = 0

Clear out your options like this in IE: 在IE中清除这样的选项:

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

or do this (fastest way to clear): 或执行以下操作(最快的清除方法):

select.innerHTML = "";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM