在 FF 和 Chrome 中接收到 JavaScript 函数的 TypeError

Receiving a TypeError in FF and Chrome for JavaScript Function

请帮助...我已尝试在 Firefox 和 Chrome 中纠正此错误。当我 运行 我的功能时,我收到当前错误。 "TypeError: obj1.options is not a function"。我已经尝试了很多事情,包括甚至尝试将 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 是一个 HTMLSelectElement 对象。 HTMLSelectElement 没有 options 方法,因此以下是不正确的:

obj.options(nCount)    // XXX

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

Returns a HTMLOptionsCollection containing the set of <option> elements contained by this element.

HTMLOptionsCollection有两种方法:

  • item(index) - returns the specific node at the given zero-based index (gives null if out of range)
  • namedItem(name) - returns the specific node with the given DOMString (i.e., string) id. Returns null if no such named node exists.

这意味着您应该使用

obj.options.item(nCount)

HTMLOptionsCollectionparent class

In JavaScript, code that accesses HTMLCollection object, in order to get an item of the given HTMLCollection, the square bracket syntax can be used instead of directly calling the item() or namedItem() methods. Numeric values in the square brackets will work the same way as item(), string values will work the same way as namedItem().

这意味着您也可以使用

obj.options[nCount]