In questo semplice script la prima funzione aggiunge dei text input inserendoli nel DOM.
La seconda funzione controlla il form verificando che gli input esistenti contengano dei valori numerici.
Lanciando il codice sotto riportato si vede che MSIE è in grado di controllare solo gli input statici presenti nell'HTML, non quelli generati dinamicamente nel DOM.
Firefox invece legge correttamente gli oggetti inseriti successivamente nel DOM.
Siccome entrambi i browser utilizzati, alle versioni 7.0 (MSIE) e 1.5.0.10 (Firefox) supportano il DOM (anche se con qualche variazione di interpretazione), appare anomalo il comportamento di MSIE e non ho trovato informazioni o riscontri a riguardo.
Potete darmi qualche feedback please o suggerire qualche hack?
Prova il file demo.
codice:
<html>
<head>
<title>MSIE fails</title>
<script language="javascript" type="text/javascript">
<!--
var count = 3; // fields already existing;
function addField() {
var d = document;
if (count < 10) {
count++;
var newLi = d.createElement('li');
d.getElementById('fieldsList').appendChild(newLi);
var text2 = d.createTextNode('Value: ');
var newValue = d.createElement('input');
newValue.setAttribute('type', 'text');
newValue.setAttribute('name', 'value_' + count);
newValue.setAttribute('size', '2');
newLi.appendChild(text2);
newLi.appendChild(newValue);
}
else {
alert('Maximum 10 fields allowed.');
}
}
function checkFields() {
var counter = 1;
var msg = '';
while (counter <= 10) {
var objStr = "document.xForm.value_" + counter;
var obj = eval(objStr);
if (typeof(obj) != 'undefined') { // if the object exists:
var objValue = parseFloat(obj.value); // the value is typecasted, but returns NaN (false) if not numeric;
if (!objValue) {
msg += 'Field #' + counter + ' is NOT numeric!\n';
} else { msg += 'Field #' + counter + ' ok.\n'; }
} else { msg += objStr + ' does not exist.\n'; }
counter++;
}
if (msg != '') {
alert(msg);
return false;
} else {
alert('OK');
return true;
}
}
//-->
</script>
</head>
<body>
<h1>MSIE DOM fails (FireFox does not)</h1>
Click "checkField" button to check if the existing values are numbers.
Add new field (max 10 fields).
<form name="xForm">
Enter numeric values (integer or double):
- Value: <input type="text" size="2" name="value_1" />
- Value: <input type="text" size="2" name="value_2" />
- Value: <input type="text" size="2" name="value_3" />
<input type="button" value="checkFields" onclick="checkFields();" /> <input type="reset" value="reset values" />
</form>
</body>
</html>