[英]Unable to get property 'selectedIndex' of undefined or null reference
运行以下javascript代码时,我在IE 11中收到以下错误,该代码使我可以从弹出列表框中选择项目并保存。 这可以在IE 9中正常运行。
function passMultiList(src, selected)
{
dst = parent.frames['downright'].document.getElementById('chosen');
var src_array = src.split("_");
for(g=0; g<src_array.length; g++){
src_array[g] = document.getElementById(src_array[g]);
if(src_array[g].selectedIndex < 0){ alert("Please select a " + src_array[g].name); return;}
}
for(i=0; i<src_array[0].options.length; i++){
if(src_array[0].options[i].selected==true){
var text = src_array[0].options[i].text;
var value = src_array[0].options[i].value;
if(src_array.length==1){
flag = 0;
for(k=0; k<dst.options.length; k++)
{
if(dst.options[k].value == value)
{
flag=1;
}
}
if(flag==0)
{
dst.options[dst.options.length]=new Option(text, value);
if(selected == 1)
{
dst.options[dst.options.length-1].selected=true;
}
}
} else {
passList(src_array, 0, text, value, selected);
}
}
}
}
在下一行引发错误
if(src_array[g].selectedIndex < 0){ alert("Please select a " + src_array[g].name); return;}
无法获取未定义或空引用的属性“ selectedIndex”
任何帮助,将不胜感激。
更新:请在下面找到(完整的)相关代码,其中包括您指导我进行的更改:
function passMultiList(src, selected)
{
dst = parent.frames['downright'].document.getElementById('chosen');
var src_array = src.split("_");
for(g=0; g<src_array.length; g++){
src_array[g] = document.getElementById(src_array[g]);
if(src_array[g] != undefined) {
if(src_array[g].selectedIndex < 0){ alert("Please select a " + src_array[g].name); return;}
}
}
var src_pushed = [];
if(src_array[g] != undefined) {
src_pushed.push(src_array[g]);
if(src_array[g].selectedIndex < 0){ alert("Please select a " + src_array[g].name); return;}
}
if(src_array[0] != undefined) {
for(i=0; i<src_pushed[0].options.length; i++){
if(src_pushed[0].options[i].selected==true){
var text = src_pushed[0].options[i].text;
var value = src_pushed[0].options[i].value;
if(src_pushed.length==1){
flag = 0;
for(k=0; k<dst.options.length; k++)
{
if(dst.options[k].value == value)
{
flag=1;
}
}
if(flag==0)
{
dst.options[dst.options.length]=new Option(text, value);
if(selected == 1)
{
dst.options[dst.options.length-1].selected=true;
}
}
} else {
passList(src_array, 0, text, value, selected);
}
}
}
}
}
function passList(src_array, current, text, value, selected){
var y = current +1;
for(j=0; j<src_array[y].options.length; j++){
if(src_array[y].options[j].selected ==true){
var atext = text + " and " + src_array[y].options[j].text;
var avalue = value + "_" + src_array[y].options[j].value;
if(y == src_array.length-1){
flag = 0;
for(k=0; k<dst.options.length; k++)
{
if(dst.options[k].value == avalue)
{
flag=1;
}
}
if(flag==0)
{
dst.options[dst.options.length]=new Option(atext, avalue);
if(selected == 1)
{
dst.options[dst.options.length-1].selected=true;
}
}
}else{
passList(src_array, y, atext, avalue, selected);
}
}
}
}
/*
function passMultiList(src1, src2, selected)
{
dst = parent.frames['downright'].document.getElementById('chosen');
if(src1.selectedIndex < 0){ alert("Please select a " + src1.name); return;}
if(src2.selectedIndex < 0){ alert("Please select a " + src2.name); return;}
for(i=0; i<src1.options.length; i++){
if(src1.options[i].selected==true){
for(j=0; j<src2.options.length; j++){
if(src2.options[j].selected==true){
var text = src1.options[i].text + "-" + src2.options[j].text;
var value = src1.options[i].value + "_" + src2.options[j].value;
flag = 0;
for(k=0; k<dst.options.length; k++)
{
if(dst.options[k].value == value)
{
flag=1;
}
}
if(flag==0)
{
dst.options[dst.options.length]=new Option(text, value);
if(selected == 1)
{
dst.options[dst.options.length-1].selected=true;
}
}
}
}
}
}
}
*/
function passSingleList(srcName, selected)
{
src = document.getElementById('single');
dst = parent.frames['downright'].document.getElementById('chosen');
if(src.selectedIndex < 0){ alert("Please select a " + srcName); return;}
for(j=0; j<src.options.length; j++)
{
if(src.options[j].selected==true)
{
flag = 0;
for(k=0; k<dst.options.length; k++)
{
if(dst.options[k].value == src.options[j].value)
{
flag=1;
if(selected == 1)
{
dst.options[k].selected=true;
}
}
else
{
if(selected == 1)
{
dst.options[k].selected=false;
}
}
}
if(flag==0)
{
dst.options[dst.options.length]=new Option(src.options[j].text, src.options[j].value);
if(selected == 1)
{
dst.options[dst.options.length-1].selected=true;
}
}
}
}
}
您的来源似乎包含部分,这些部分在行中无法选择:
src_array[g] = document.getElementById(src_array[g]);
为了摆脱这个问题,您可以插入以下内容:
src_array[g] = document.getElementById(src_array[g]);
if(src_array[g] != undefined) {
if(src_array[g].selectedIndex < 0){ alert("Please select a " + src_array[g].name); return;}
}
如果您的问题不是错误的src条目,这将解决您的问题。
当然,您还需要更改其他代码-例如,您不应该直接使用src_array [0]-而不检查它是否未定义
更新:由于我不知道您的代码的目的,我将尽我所能完成它-重要:这不能解决您的第一个元素未定义的事实。 src中的某些内容有误或该元素不存在,即使应该存在也是如此。
function passMultiList(src, selected)
{
dst = parent.frames['downright'].document.getElementById('chosen');
var src_array = src.split("_");
for(g=0; g<src_array.length; g++){
src_array[g] = document.getElementById(src_array[g]);
if(src_array[g] != undefined) {
if(src_array[g].selectedIndex < 0){ alert("Please select a " + src_array[g].name); return;}
}
}
if(src_array[0] != undefined) {
for(i=0; i<src_array[0].options.length; i++){
if(src_array[0].options[i].selected==true){
var text = src_array[0].options[i].text;
var value = src_array[0].options[i].value;
if(src_array.length==1){
flag = 0;
for(k=0; k<dst.options.length; k++)
{
if(dst.options[k].value == value)
{
flag=1;
}
}
if(flag==0)
{
dst.options[dst.options.length]=new Option(text, value);
if(selected == 1)
{
dst.options[dst.options.length-1].selected=true;
}
}
} else {
passList(src_array, 0, text, value, selected);
}
}
}
}
}
但是,您可以尝试通过将正确的条目推入新数组来解决此问题:
var src_pushed = [];
...
if(src_array[g] != undefined) {
src_pushed.push(src_array[g]);
if(src_array[g].selectedIndex < 0){ alert("Please select a " + src_array[g].name); return;}
}
...
for(i=0; i<src_pushed[0].options.length; i++){
...
我终于弄清楚是什么原因造成的:
IE 11希望通过ID,而旧版本的IE则不需要。
为了解决这个问题,我更改了:
<option value="{entity_id}"><xsl:value-of select="name"/></option>
至
<option **id="src_array[g]"** value="{entity_id}"><xsl:value-of select="name"/></option>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.