Why is my for loop stopping after 5 iterations? (in function hello())
<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
}
}
}
开发者_如何学C
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í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í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ó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>
Because
document.getElementById("producto").length
== 5.
By the looks of things:
<select id="producto" name="producto" style="display:block;margin-left:20px; margin-top:-14px">
<option value="FALSE" selected="selected">- Elige el artículo -</option>
<?=$productos?>
<option value="FALSE" onClick="addp()">..Añadir producto..</option>
</select><br />
the result may very well be "FALSE" - which has a length of 5.
It looks like you want to iterate over the options for the select. You need to reference that property. Note that you don't need to use getElementById again since you just retrieved it.
var totalproductos = selectproducto.options.length;
精彩评论