【问题标题】:Receiving a TypeError in FF and Chrome for JavaScript Function在 FF 和 Chrome 中为 JavaScript 函数接收 TypeError
【发布时间】:2015-03-20 21:20:54
【问题描述】:

请帮助...我已尽一切努力纠正 Firefox 和 Chrome 中的此错误。我在运行我的函数时收到当前错误。 “TypeError:obj1.options 不是函数”。我已经尝试了很多事情,包括甚至尝试将 onclick 函数更改为 JQuery .click() 函数以及放置在 JQuery Document.ready 函数之间。它在 IE 中运行良好,但在其他浏览器中抛出 TypeError。实际上我得到了多个类型错误,一个是 AddMember 函数,一个是 Remove Member。这可能是旧的不推荐使用的 JavaScript。提供的任何帮助表示赞赏。谢谢

function RemoveMember(idx, idx2){
//centralizes code to remove selected items from a MULTIPLE SELECT (listbox).
//loops "backward" to maintain valid indexing counters while removing items.

var obj = idx;
var obj2 = idx2;
var nCount = 0;
    nCount = obj.options.length - 1;        
    for ( nCount; nCount > -1; nCount -- ) {
        if ( obj.options(nCount).selected == true ) {               
            var oOption = document.createElement("OPTION"); 
            oOption.text = obj.options(nCount).text;
            oOption.value = obj.options(nCount).value;
            obj.options.remove(nCount);
            //JJK added add back to avail list 
            obj2.add(oOption);                          
        }               
    } //for 

    //sort both listboxes
    sortOptions(obj2);
    sortOptions(obj);       
}

这是完整的 html 文件,您可以查看所有内容。我们基本上有两个列表框,并且有两个带有单击操作的图像,可以将列表项从左侧的第一个列表集中添加或删除到第二个列表集中。

<!-- #include file = "../include/permissions.inc" -->
<!-- #include file = "../Include/adovbs.inc" -->

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<%
CheckPermissions pManagePFI, True
Dim sType, PFIName
Dim objSup, osRS, ret, iSupID

sType = Request.QueryString ("type")
pfid = Request.Form ("pfid")
'Set Type
if sType = "SV" then
	sType = "2"
elseif sType = "DC" then
	sType = "1"
end if
'Get PFI Name	
if not pfid = "" then
	set objPFI = Server.CreateObject("LAS.NET.CPFI") 					
	PFIName  = objPFI.GetName(clng(pfid)) 								
	set objPFI=nothing			
end if			

function selOptions()
	Response.Write "<option value='0'>select</option>"
	if sType = "2" then
		Response.Write "<option selected value='2'>Servicer</option>"
	else
		Response.Write "<option value='2'>Servicer</option>"
	end if
						    			
	if sType = "1" then
		Response.Write "<option selected value='1'>Document Custodian</option>"
	else
		Response.Write "<option value='1'>Document Custodian</option>"
	end if
end function

function GetSupporters()
			set objSup = Server.CreateObject("LAS.NET.CSupporter") 
			Dim sSupportName, getType
			
			if sType = "2" then
				getType = "Servicer"
			elseif sType = "1" then
				getType = "Document Custodian"
			end if
			ret = objSup.GetSupportList(cstr(getType), clng(pfid)) 
				
			Set osRS = UnPersistRS(ret)
			if not osRS.EOF then			
				osRS.MoveFirst 
				osRS.Filter = "bActive = True"
					Do while not osRS.EOF
						sSupportName = osRS.Fields("sSupport_Name").Value
						iSupID = osRS.Fields("iSupport_ID").Value
						Response.write "<option value='" & iSupID & "'>" & sSupportName & "</option>"
						osRS.MoveNext
					loop
			end if
			set osRS=nothing
			set objSup=nothing
end function

function GetPFISupporters()
	if not pfid = "" then	
		set objPFI = Server.CreateObject("LAS.NET.CPFI") 
        dim osRS 
		if sType="2" then
			ret = objPFI.GetSupporters("Servicer",clng(pfid))
		elseif sType= "1" then
			 ret = objPFI.GetSupporters("Document Custodian",clng(pfid))
		end if
		If len(ret) <> 0 then
			Set osRS = UnPersistRS(ret)

			if not osRS.eof then	
				Do while not osRS.eof	
					sSupportName = osRS.Fields("sSupport_Name").Value
					iSupID = osRS.Fields("iSupport_ID").Value
					Response.write "<option value='" & iSupID & "'>" & sSupportName & "</option>"
					osRS.MoveNext
				loop
			end if
		end if
		set osRS=nothing
		set objPFI=nothing
	end if
end function%>

<title>FHLB</title>

<script language="javascript">



function SelectValue(idx, val){
//centralizes code to loop index values in SELECT and highlight
//appropriate item.

var obj = idx;
	var nCount = 0;
	var nCounter = 0;
		nCount = obj.options.length;
		for ( nCounter = 0; nCounter < nCount; nCounter ++ ) {
			if ( obj.options(nCounter).value == val ) {		
					obj.selectedIndex = nCounter;
				}
			}
			obj.value = obj.options( obj.selectedIndex ).value ; 
}

function OneSelected(idx){
//function to search a fieldset, and determine whether any checkboxes
//have been selected.
var oRows = idx.children(0).children(0).children	//the only child of the fieldset is the TABLE.
var nCount = idx.children(0).children(0).children.length;
var nCounter = 0;
var ret = false;
	for ( nCounter = 0; nCounter < nCount; nCounter ++ ) {
		if ( oRows(nCounter).children(1).children(0).checked == true ) {		
			ret = true;
			break;
		}
	}
	if ( ret == true ){
		return true;
	}
	else{
		return false;
	}
}

function Validate() {
	//alert("Attempted save");
	
	var obj = document.frmFHLBGlobal.MemberList ; 
	var nCount = 0; 
	var nCounter = 0; 

	nCount = obj.options.length; 
	for ( nCounter = 0; nCounter < nCount; nCounter ++ ) { 
		obj.options(nCounter).selected = true; 
	} 
	document.frmFHLBGlobal.action="process_AssignSupporter.asp";
	document.frmFHLBGlobal.submit();
}

function Refresh() {
	var obj = document.all.selType;
	obj.value = obj.options( obj.selectedIndex ).value;
	document.frmFHLBGlobal.action = "AssignSupporter.asp?type=" + obj.value;
	document.frmFHLBGlobal.submit();
}

function AddMember(idx1, idx2){
//centralizes code to loop index values in SELECT and highlight
//appropriate item.
var obj1 = idx1;
var obj2 = idx2;
var nCount1 = 0;
var nCounter1 = 0;
var nCount = 0;
		//add entries to the assigned box
		nCount1 = obj1.options.length;
		for ( nCounter1 = 0; nCounter1 < nCount1; nCounter1 ++ ) {
			if ( obj1.options(nCounter1).selected == true ) {					
				var oOption = document.createElement("OPTION");	
				oOption.text = obj1.options(nCounter1).text;
				oOption.value = obj1.options(nCounter1).value;
				obj2.add(oOption);	
			}			
		} 
		//remove entries from available box		
		//loops "backward" to maintain valid indexing counters while removing items.
		nCount = obj1.options.length - 1;		
		for ( nCount; nCount > -1; nCount -- ) {			
			if ( obj1.options(nCount).selected == true ) {							
				obj1.options.remove(nCount);			
			}
		}
		
		//sort both listboxes
		sortOptions(obj2);
		sortOptions(obj1);	
}


function RemoveMember(idx, idx2){
//centralizes code to remove selected items from a MULTIPLE SELECT (listbox).
//loops "backward" to maintain valid indexing counters while removing items.

var obj = idx;
var obj2 = idx2;
var nCount = 0;
	nCount = obj.options.length - 1;		
	for ( nCount; nCount > -1; nCount -- ) {
		if ( obj.options(nCount).selected == true ) {				
			var oOption = document.createElement("OPTION");	
			oOption.text = obj.options(nCount).text;
			oOption.value = obj.options(nCount).value;
			obj.options.remove(nCount);
			//JJK added add back to avail list 
			obj2.add(oOption);							
		}				
	} //for	
	
	//sort both listboxes
	sortOptions(obj2);
	sortOptions(obj);		
}

//jjk sort add V2.3.2
function sortOptions(listbox) {
 
 //get listbox values into arrays 
 var arrValues = new Array()
 var arrText = new Array()
 var obj = listbox;
 var nCount = 0;
 var nCounter = 0;
	nCount = obj.options.length;
	for ( nCounter = 0; nCounter < nCount; nCounter ++ ) {						
		arrText[nCounter]= obj.options(nCounter).text;		
		arrValues[nCounter] = obj.options(nCounter).value;						
	} 
 
 var x, y, holder, temp_text1, temp_text2;
 // The Bubble Sort method.
 for(x = 0; x < arrText.length; x++) {
    for(y = 0; y < (arrText.length-1); y++) {
    temp_text1 = arrText[y];
    temp_text2 = arrText[y+1];
      if(temp_text1.toUpperCase() > temp_text2.toUpperCase()) {
        holder = arrText[y+1];
        arrText[y+1] = arrText[y];
        arrText[y] = holder;
        //do same for values array to keep their indexes together
        holder = arrValues[y+1];
        arrValues[y+1] = arrValues[y];
        arrValues[y] = holder;
      }
    }
  }

  // Update the select box list.
	var i;
	for(i = 0; i < arrValues.length; i++) {
	  if(listbox.options[i] == null) {
	    var oOption = document.createElement("OPTION");	
			oOption.text = arrText[i];
			oOption.value = arrValues[i];			
			listbox.options[i] = oOption;
	  } 
	  else {
	    listbox.options[i].text = arrText[i];
	    listbox.options[i].value = arrValues[i];
	  }
	}
} //jjk sort end

function goPFI(){
	document.frmFHLBGlobal.action ="ViewPFI.asp";
	document.frmFHLBGlobal.submit();  
}

</script>
<link rel="stylesheet" type="text/css" href="../Style/<%=application("DistrictCode")%>.css">
</head>
<body>

<!-- #include file = "../include/head.inc" -->
<p>
<table align="center" valign="middle" width="50%" border="1" class="InnerTable">
				<tr>
   					<td class="tableheader" width="100%">
   						<center><font class="tableheadertitletext">Add Supporter for <%=PFIName%></font></center>
   					</td>	
				</tr>
				<tr><td align="center">				
					<form name="frmFHLBGlobal" method="Post" action="process_AssignSupporter.asp">
					<input type="hidden" name="pfid" value="<%=pfid%>">
					<table width="100%" cellpadding="0" cellspacing="0">
					
					<input type="hidden" name="Test">
					<tr>
					<td align="center">
					 					
						<fieldset><!------list name and type-------->
						<table class="InnerTable" width="100%" border="0">
							<tr>
								<td class="InnerTableTextSmall">
						     		&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
						     		&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
						     		<b>List Type:</b>&nbsp;
						    		<select name="selType" onchange="Refresh()">
						    			<%=selOptions%>
						    		</select>
						     	</td>
							</tr>
						</table>
						</fieldset>
						
						<fieldset><!------listbox select-------->
						<table class="InnerTable" width="100%" border="0">
							<tr>
								<td width="45%" align="center" class="InnerTableTextSmall">
								<b>Available<b><br>
									<select name="MainList" MULTIPLE style="width: 200" size="15" ondblclick="AddMember(document.frmFHLBGlobal.MainList, document.frmFHLBGlobal.MemberList);">
										<%
										'if len(trim(strOptions)) > 0 then
											Response.Write GetSupporters()										
										'end if
										%>
									</select>
									
								</td>
								<td width="10%" align="center" valign="middle">
									<img src="../images/<%=application("DistrictCode")%>/add2.gif" name="Add Member(s)" id="Add" onclick="AddMember(document.frmFHLBGlobal.MainList, document.frmFHLBGlobal.MemberList);">	
									<img src="../images/<%=application("DistrictCode")%>/remove.gif" name="Remove Member(s)" id="Remove" onclick="RemoveMember(document.frmFHLBGlobal.MemberList, document.frmFHLBGlobal.MainList);">	
								</td>
								<td width="45%" align="center" class="InnerTableTextSmall">
									<b>Currently Assigned<b><br>
									<select name="MemberList" MULTIPLE style="width: 200" size="15" ondblclick="RemoveMember(document.frmFHLBGlobal.MemberList);">
										<%=GetPFISupporters()%>
									</select>
									
								</td>
							</tr>
						</table>
						</fieldset>
						<fieldset>
						<table class="InnerTable" width="100%" border="0">
						<tr>
							<td align="center">
								<img src="../images/<%=application("DistrictCode")%>/save.gif" name="Save" id="Save" onclick="Validate()">	
								<img src="../images/<%=application("DistrictCode")%>/cancel.gif" name="Cancel" id="Cancel" onclick="goPFI()">	
							</td>
						</tr>
						</table>
						</fieldset>
					</td>
					</tr>
					</table>	
					</td>
				</tr>
			</table>
			<input type="hidden" name="sType" value="<%=sType%>">
		</form>
<!-- #include file = "../include/foot.inc" -->
</body>
</html>

【问题讨论】:

  • obj.options(nCount) -> obj.options[nCount]
  • 您对错误有什么不明白的地方?这对我来说似乎很清楚。 obj.options 不是函数,所以不要把它当成函数。
  • 感谢 cmets,我对 JavaScript 还很陌生,正在尝试升级在现代浏览器出现之前构建的系统。我认为这不是一个功能,但不知道如何解决这个问题。感谢您的回复。

标签: javascript function onclick typeerror deprecated


【解决方案1】:
obj.options(nCount)

应该是

obj.options[nCount]

obj 是一个 HTMLSelectElement 对象。 HTMLSelectElement 没有 options 方法,所以下面的说法不正确:

obj.options(nCount)    // XXX

HTMLOptionsCollection 对象确实有一个 options 属性,记录如下:

返回一个HTMLOptionsCollection,其中包含该元素所包含的&lt;option&gt; 元素集。

HTMLOptionsCollection有两种方法:

  • item(index) - 返回给定从零开始的索引处的特定节点(如果超出范围,则返回 null)
  • namedItem(name) - 返回具有给定 DOMString(即字符串)id 的特定节点。如果不存在这样的命名节点,则返回 null。

这意味着你应该使用

obj.options.item(nCount)

HTMLOptionsCollectionparent class

在 JavaScript 中,访问HTMLCollection 对象的代码,为了获取给定HTMLCollection 的项目,可以使用方括号语法来代替直接调用item()namedItem() 方法。方括号中的数值与item() 的工作方式相同,字符串值与namedItem() 的工作方式相同。

这意味着你也可以使用

obj.options[nCount]

【讨论】:

  • 谢谢,我会试一试。有很多地方我需要替换这段代码,但这很有帮助,让我能够理解问题。
  • 使用基于正则表达式的搜索和替换应该不会太难修复。
  • 感谢用括号 [ ] 替换括号 ( ) 的工作。也非常感谢您的快速响应。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-12-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-11-05
相关资源
最近更新 更多