多选列表组件

多选列表组件HTML:
 <form METHOD=POST name="form1" ACTION="" onSubmit="return false">
     <table border=0 cellpadding="0"  align="center" cellspacing="1" >
      <tr align="center" >
       <td><div align="center">
         <table>
          <tr><td align="center">可选的业务列表</td>
      <td></td>
     <td align="center">已选的业务列表</td></tr>
          <tr><td id="canChoose"></td>
           <td><div align='center'><p align='center'>
            <input type='hidden' readonly='true' name='fieldlist_id'>
            <input type='button'  value='添加&gt;&gt;&gt;' class="buttoncss" OnClick="JavaScript:AddItemNM('Field','yesfield', ' ')">
            <br><input type='button'  value='&lt;&lt;&lt;删除'  class="buttoncss" OnClick="JavaScript:DeleteItem('yesfield')">
            </p></div>
           </td><td id="hasChoose"></td></tr>
         </table></div> </td></tr></table></form>
JS:

function GetObjID(ObjName)
{
  for (var ObjID=0; ObjID < window.form1.elements.length; ObjID++)
    if ( window.form1.elements[ObjID].name == ObjName )
    {  return(ObjID);
       break;
    }
  return(-1);
}
function AddItemNM(ObjName, DesName, CatName)
{

 //ObjName--可选列表;DesName--已选列表;CatName--选择要添加或删除的项
  //GET OBJECT ID AND DESTINATION OBJECT
  ObjID    = GetObjID(ObjName);
  DesObjID = GetObjID(DesName);
  k=0;
  i = document.form1.elements[ObjID].options.length; //可选列表里项的个数
  if (i==0)
    return;
  maxselected=0
  for (h=0; h<i; h++)
     if (document.form1.elements[ObjID].options[h].selected ) {
         k=k+1;
         maxselected=h+1;
         }
  if (maxselected>=i)
     maxselected=0;
 // if ( document.form1.elements[DesObjID].length + k >100 ) {
 //   window.alert("最多可选择5条");
 //   return;
 //   }

  if (CatName != "")
    CatObjID = GetObjID(CatName);
  else
    CatObjID = 0;
  if ( ObjID != -1 && DesObjID != -1 && CatObjID != -1 )
  { jj = document.form1.elements[CatObjID].selectedIndex;
    if ( CatName != "")
    { CatValue = document.form1.elements[CatObjID].options[jj].text;
      CatCode  = document.form1.elements[CatObjID].options[jj].value;
    }
    else
      CatValue = "";
    i = document.form1.elements[ObjID].options.length;
    j = document.form1.elements[DesObjID].options.length;
    for (h=0; h<i; h++)
    { if (document.form1.elements[ObjID].options[h].selected )
      {  Code = document.form1.elements[ObjID].options[h].value;
         Text = document.form1.elements[ObjID].options[h].text;
         j = document.form1.elements[DesObjID].options.length;
         if (Text.indexOf('--')!=-1) {
            for (k=j-1; k>=0; k-- ) {
              document.form1.elements[DesObjID].options[k]=null;
            }
            j=0;
         }
         if (Text.substring(0,1)=='-' && Text.substring(1,2)!='-') {
            for (k=j-1; k>=0; k-- ) {
              if (((document.form1.elements[DesObjID].options[k].value).substring(0,2))==(Code.substring(0,2)))
                 document.form1.elements[DesObjID].options[k]=null;
            }
            j= document.form1.elements[DesObjID].options.length;
         }
         HasSelected = false;
         for (k=0; k<j; k++ ) {
           if ((document.form1.elements[DesObjID].options[k].text).indexOf('--')!=-1){
              HasSelected = true;
              window.alert('已经包括本选项:'+Text);
              break;
           }else if ((document.form1.elements[DesObjID].options[k].text).indexOf('-')!=-1 && ((document.form1.elements[DesObjID].options[k].value).substring(0,2)==Code.substring(0,2))){
              HasSelected = true;
              window.alert('已经包括本选项:'+Text);
              break;
           }
           if (document.form1.elements[DesObjID].options[k].value == Code)
           {  HasSelected = true;
              break;
           }
         }
         if ( HasSelected == false)
         { if (CatValue !="")
           { Location = GetLocation(DesObjID, CatValue);
             if ( Location == -1 )
             { document.form1.elements[DesObjID].options[j] =  new Option("---"+CatValue+"---",CatCode);
               document.form1.elements[DesObjID].options[j+1] = new Option(Text, Code);
             }//if
             else
             { InsertItem(DesObjID, Location+1);
               document.form1.elements[DesObjID].options[Location+1] = new Option(Text, Code);
             }//else
           }
           else
             document.form1.elements[DesObjID].options[j] = new Option(Text, Code);
         }//if
         document.form1.elements[ObjID].options[h].selected =false;
       }//if
    }//for
    document.form1.elements[ObjID].options[maxselected].selected =true;
  }//if
}//end of function


function DeleteItem(ObjName)
{
  ObjID = GetObjID(ObjName);
  minselected=0;
  if ( ObjID != -1 )
  {
    for (i=window.form1.elements[ObjID].length-1; i>=0; i--)
    {  if (window.form1.elements[ObjID].options[i].selected)
       { // window.alert(i);
          if (minselected==0 || i<minselected)
            minselected=i;
          window.form1.elements[ObjID].options[i] = null;
       }
    }
    i=window.form1.elements[ObjID].length;
  }
}

posted @ 2011-05-03 14:32  跳刀的兔子  阅读(336)  评论(0编辑  收藏  举报