先看代码:
    <table width="60%" border="0" align="center">
      <tr>
        <td width="21%" align="center">待选小区</td>
        <td width="49%">&nbsp;</td>
        <td width="30%" align="center">已选小区</td>
      </tr>
      <tr>
        <td rowspan="7">
            <select size="10" id="sleft" multiple></select>
        </td>
        <td>&nbsp;</td>
        <td rowspan="7"><form id="form1" name="form1" method="post" action="">
          <label>
            <select size="10" id="sright" multiple></select>
          </label>
        </form>
        </td>
      </tr>
      <tr>
        <td></td>
        </tr>
      <tr>
        <td align="center"><input type="image" id="btn1" src="image/bt1.bmp" onclick="moveRight()" /></td>
        </tr>
      <tr>
        <td align="center"><input type="image" id="btn2" src="image/bt2.bmp" onclick="moveRightAll()" /></td>
        </tr>
      <tr>
        <td align="center"><input type="image" id="btn3" src="image/bt3.bmp" onclick="moveLeft()"/></td>
        </tr>
      <tr>
        <td align="center"><input type="image" id="btn4" src="image/bt4.bmp" onclick="moveLeftAll()"/></td>
        </tr>
      <tr>
        <td>&nbsp;</td>
        </tr>
    </table>下面是4个按钮对应的js代码:
function moveRight()
{
var leftsel = document.getElementById("sleft");
var rightsel = document.getElementById("sright");
var options = leftsel.options; for(var i=0;i<options.length;i++)
{
if(options[i].selected)
{
rightsel.appendChild(options[i]);
}
}
}
function moveRightAll()
{
var leftsel = document.getElementById("sleft");
var rightsel = document.getElementById("sright");
var options = leftsel.options;
while(options.length>0)
{
rightsel.appendChild(options[0]);
}}
function moveLeft()
{
var leftsel = document.getElementById("sleft");
var rightsel = document.getElementById("sright");
var options = rightsel.options;
for(var i=0;i<options.length;i++)
{
if(options[i].selected)
{
leftsel.appendChild(options[i]);
}
}
}
function moveLeftAll()
{
var leftsel = document.getElementById("sleft");
var rightsel = document.getElementById("sright");
var options = rightsel.options;
while(options.length>0)
{
leftsel.appendChild(options[0]);
}}<myxml>
<area name="沧浪区" value="2000">
<community name="常福名苑" value="2001"></community>
<community name="颜岗南村" value="2002"></community>
<community name="元和五区" value="2003"></community>
</area>
<area name="金阊区" value="3000">
<community name="黄家新村" value="3001"></community>
<community name="狄家浜" value="3002"></community>
<community name="泰安下塘" value="3003"></community>
</area>
<area name="平江区" value="4000">
<community name="红枫苑" value="4001"></community>
<community name="明日星洲" value="4002"></community>
<community name="桐泾新村" value="4003"></community>
<community name="常福一村" value="4004"></community>
</area>
<area name="相城区" value="5000">
<community name="腰娄北" value="5001"></community>
<community name="九里苑" value="5002"></community>
<community name="烟草小区" value="5003"></community>
</area>
<area name="高新区" value="6000">
<community name="嵩山路杜家村" value="6001"></community>
<community name="义庄公寓" value="6002"></community>
<community name="丽江苑" value="6003"></community>
</area>
<area name="吴中区" value="7000">
<community name="山湖苑" value="7001"></community>
<community name="尚湖山庄" value="7002"></community>
<community name="明星新城" value="7003"></community>
<community name="八仙桥" value="7004"></community>
<community name="报慈北路" value="7005"></community>
<community name="海华苑" value="7006"></community>
</area>
<area name="工业园区" value="8000">
<community name="三里桥东" value="8001"></community>
<community name="恒悦二区" value="8002"></community>
<community name="花园西村" value="8003"></community>
<community name="共青小区" value="8004"></community>
<community name="宾曦苑" value="8005"></community>
</area>
</myxml>我的问题是:
点击btn1,进行一次验证,若左列表为空或未选中对象则弹出提示,且终止操作
点击btn2,进行一次验证,左列表为空则弹出提示,终止操作
如果选中的选项与右列表中内容重复,弹出提示
请教各位大侠

解决方案 »

  1.   


    function moveRight()
    {
        var leftsel = document.getElementById("sleft");
        var rightsel = document.getElementById("sright");
        var options = leftsel.options;
        if(options.length == 0 || options.selectedIndex == -1){
         alert("The selected content can't be null!");
        }
        else{
         var selectedContent = options[options.selectedIndex];
         if(!rightsel.options.length){
         rightsel.appendChild(selectedContent);
         }else{
         var rightOptions = rightsel.options;
         for(var i = 0;i < rightOptions.length; i ++){
         if(selectedContent.firstChild.nodeValue == rightOptions[i].firstChild.nodeValue){
         alert("The selected has already exist in right selection!");
         return;
         }
         }
         rightsel.appendChild(selectedContent);
         }
        }
    }
    function moveRightAll()
    {
        var leftsel = document.getElementById("sleft");
        var rightsel = document.getElementById("sright");
        var options = leftsel.options;
        if(options.length == 0){
         alert("The selected content can't be null!");
        }
        while(options.length>0)
        {
            rightsel.appendChild(options[0]);
        }}