运行至alert(answer[i].value)网页报“vlaue为空或不是对象”,我这种取值方式有问题吗?还是那里写法有错误?
          <select size=1 name="sd_carboncopy" id="sd_carboncopy">  
  <option value="">无</option>
   <%do while not rs.eof%>     
          <option value="<%=rs("name")%>"><%=rs("name")%></option>
  <%
  rs.movenext
  loop
  rs.close
          %>
          </select> 
<input  type="button" value="add" onClick="setname('td_carboncopy');">
function setname(obj)
{
    var copy=document.getElementById("sd_carboncopy").value;
var answer = document.getElementsByName("cp");
    if (copy != ""){
          var i = answer.length
  if (i > 0 )
   {
   alert(answer[i].value);
   }
   // for (var i = answer.length-1; i >= 0; i--) {
         //if (copy != answer[i].value )
                {
                 document.getElementById(obj).innerHTML += "<input type='checkbox' name='cp' />" + copy ;
                 }
                 // else
    // {
    // alert("对不起,你选择了重复的对象!");
// }
//}
}
 }

解决方案 »

  1.   

    var i = answer.length
                      if (i > 0 )
                       {
                       alert(answer[i].value);
                       }
    数组超界了
      

  2.   

    document.getElementsByName("cp"); 执行时候 cp存在?
      

  3.   

    这个就是添加 “cp”的啊
                            {
                             document.getElementById(obj).innerHTML += "<input type='checkbox' name='cp' />" + copy ;
                             }
      

  4.   

    <select size=1 name="sd_carboncopy" id="sd_carboncopy">  
                <option value="">无</option>
                <option value="1">无1</option>
                <option value="2">无2</option>
                <option value="3">无3</option>
              </select> 
              <div id="td_carboncopy">
                
              </div>
            <input  type="button" value="add" onclick="setname('td_carboncopy');"/>
            <script type="text/javascript">
                function setname(obj)
                {
                    var copy=document.getElementById("sd_carboncopy").value;
                    var answer = document.getElementById(obj).getElementsByTagName("input");
                    var isRepeat = false;
                        if (copy != ""){
                                  var i = answer.length
                                  if (i > 0) {
                                      //alert(answer[i].value);
                                  }
                                  for (var i = answer.length - 1; i >= 0; i--) {
                                      if (answer[i].name == "cp") {
                                          if (copy != answer[i].value) {
                                              // document.getElementById(obj).innerHTML += "<input type='checkbox' name='cp' />" + copy ;
                                          }
                                          else {
                                              isRepeat = true;
                                              break;
                                              // alert("对不起,你选择了重复的对象!");
                                          }
                                      }
                                  }
                                  if (!isRepeat) { //如果没有重复
                                         document.getElementById(obj).innerHTML += "<input type='checkbox' name='cp' value='" + copy + "' />" + document.getElementById("sd_carboncopy").options[document.getElementById("sd_carboncopy").selectedIndex].text;
                                     }
                        }        
                 }
                 </script>
      

  5.   

    var i = answer.length;alert(answer[i].value);当然会报错了,js中数组下标从0开始到length - 1;answer[i]已经数组越界了。。answer[i] = undefined,当然没有valuealert(answer[i - 1].value);
      

  6.   

    还有你的checkbox没有设置value属性所以alert(answer[i - 1].value);可能不是你想要得结果,而是'on'
      

  7.   


    找到我的写法原因了,7楼方法也可以
    document.getElementById(obj).innerHTML += "<input type='checkbox' name='cp' value='"+copy+"'/>" + copy ;