<script language="javascript">
function hello()
{
   var selectproducto = document.getElementById("producto");

   var totalproducto = document.getElementById("producto").length;

   var selectmanufacturero = document.getElementById("manufacturero");

   var chosenoption = selectmanufacturero.options[selectmanufacturero.selectedIndex].getAttribute('value');

   var limite = totalproducto;

   for(var i = 0; i<limite; i++) {

    if(i==0){ alert('|'); }
    else if(i==7){ alert('|'); }
    else{

     var ahora =  selectproducto.options[i].getAttribute('title');
     alert(i);

     if( ahora == chosenoption){ alert('+'); }
     else{
      alert('-');
      selectproducto.options[i] = null; //saca la opcion
     }

    }
   }



   selectproducto.style.display = 'block';
}
</script>

<script language="javascript" type="text/javascript">
function addm()
{
   document.getElementById("manufacturero").style.display="none";
   document.getElementById("new_man").style.display="block";
   document.getElementById("new_product").style.display="block";
 }
</script>

<script language="javascript" type="text/javascript">
function addp()
{
   document.getElementById("producto").style.display="none";
   document.getElementById("new_product").style.display="block";
}
</script>

<?php
 // ---------------- Crea opciones a escoger para los manufactureros -----------
 $query = "SELECT * FROM manufactureros";
 $result = mysql_query($query);

 while( $row = mysql_fetch_assoc($result) )
 {
  $anterior = $manufactureros;
  $manufactureros = $anterior."<option id='manufacturero_opcion' value='".$row['name']."'>".$row['name']."</option>";
 }
 // ---------------- Crea opciones a escoger para los articulos ----------------
 $result = mysql_query("SELECT * FROM productos");

 while( $row = mysql_fetch_assoc($result) )
 {
  $anterior = $productos;
  $productos = $anterior."<option value='".$row['descripcion']."' title='".$row['manufacturero']."'>".$row['descripcion']."</option>";
 }
?>

</head>

<body>

 <form id="reservacion" action="#" method="POST" style="display:block;">

       <div><b>Art&iacute;culo</b><br />

           <select id="manufacturero" name="manufacturero" style="display:block;margin-left:20px" onchange='hello()'>
              <option value="FALSE" selected="selected">- Elige el manufacturero -</option>
              <?=$manufactureros?>
              <option value="FALSE" onClick="addm()">..Añadir manufacturero..</option>
           </select><br />

           <div id="new_man" style="display:none;margin-left:20px; margin-top:-20px; margin-bottom:-20px">Reservar con un manufacturero nuevo:<br />
             <input type="text" name="new_man" size=40 value="" style="margin-left:20px; margin-top:10px">
           </div>

           <select id="producto" name="producto" style="display:block;margin-left:20px; margin-top:-14px">
              <option value="FALSE" selected="selected">- Elige el art&iacute;culo -</option>
              <?=$productos?>
              <option value="FALSE" onClick="addp()">..Añadir producto..</option>
           </select><br />

           <div id="new_product" style="display:none; margin-left:20px; margin-bottom:20px">Reservar con un producto nuevo:
               <table style="margin-left:20px">
                   <tr><td>Id : </td><td><input type="text" name="new_product_id" size=40 value=""></td></tr>
                   <tr><td>Descripci&oacute;n : </td><td><input type="text" name="new_product" size=40 value=""></td></tr>
               </table>
          </div>

       </div>

       <input type="submit" name="submit" value="Reserva">

   </form>

</body>

最佳答案

因为

document.getElementById("producto").length


== 5。

从外观上看:

<select id="producto" name="producto" style="display:block;margin-left:20px; margin-top:-14px">
  <option value="FALSE" selected="selected">- Elige el art&iacute;culo -</option>
  <?=$productos?>
  <option value="FALSE" onClick="addp()">..Añadir producto..</option>
</select><br />


结果很可能是“ FALSE”-长度为5。

关于javascript - 为什么我的for循环在5次迭代后停止? (在函数hello()中),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/3075821/

10-09 23:22