• Aucun résultat trouvé

Using Arrays to Access the Contents of Forms 5.5.1

Dans le document Guide to HTML, JavaScript and PHP (Page 137-144)

does not change the results.

5.5

Using Arrays to Access the Contents of Forms

5.5.1

Accessing Values of type="text" Fields

Consider this generic problem: A form stores several values in <input> fields in a table. 

You want the last row of the table to hold the sum of all the previous values. Based on  previous material, you can give each form field a name: v1, v2, v3, etc. Then, you can  sum the values:

sum.value =

parseFloat(v1.value)+parseFloat(v2.value)+ …

This is not a very satisfying solution, if for no other reason than the fact that large tables  will require a lot of typing.

Fortunately, there is a more elegant alternative. When you create an HTML form, all the  elements  are  automatically  stored  in  an  array  called elements.  You  can  access  the   contents of this array just as you would the contents of any other array. Consider the fol-lowing very simple document.

Document 5.6 (formArray.htm)

<html>

<head>

<title>Using the elements[] array to access values in forms.

</title>

</head>

<body>

<form name="myform">

A[0]<input type="text" value="3" /><br />

A[1]<input type="text" value="2" /><br />

</form>

123 5.5 Using Arrays to Access the Contents of Forms

<script language="javascript" type="text/javascript">

for(var i=0; i<document.myform.elements.length; i++) { document.write("A["+i+"] =

"+document.myform.elements[i].value+"<br />");

}</script>

</body>

</html>

First of all, note that these form fields haven’t been  given names in the <input /> tags. They could have  names, but the point here is to avoid having to assign 

many different field names to something that can be treated as a unit, under a single name. 

Not surprisingly, the elements of the elements array are assigned, starting with index 0,  in the order in which they appear in the form.

Previously, forms themselves haven’t been given names. However, it is entirely possi-ble that you might wish to have more than one group of form fields in a document, each of  which would have its own elements array and could be accessed through its own name. 

Hence, the use of the name attribute in the form tag in Document 5.6. In this example,  the use of “document” in, for example,

document.myform.elements[i].value;

is optional.

Document 5.7 shows a solution to the generic problem given at the beginning of this  section.

Document 5.7 (sumForm.htm)

<html>

<head>

<title>Sum a column of values</title>

</head>

<body>

<form name="sumform">

<input type="text" value="3.3" /><br />

<input type="text" value="3.9" /><br />

<input type="text" value="7.1" /><br />

Here is the sum of all the values.<br />

<input type="text" name="sum" value="0"

/><br />

</form>

<script language="javascript" type="text/javascript">

var sum=0;

for (var i=0;

124 5 Using Arrays in HTML/JavaScript

5

i<(sumform.elements.length−1);

i++)

sum+=parseFloat(sumform.

elements[i].value);

sumform.elements[sumform.

elements.length−1].value=sum;

</script>

</body>

</html>

The terminating condition on the for… loop is i<(sumform.elements.length−1) rather than

i<sumform.elements.length

because the last element in the elements array does not contain one of the values to be  summed.

With multiple columns in a table, you will need to implement the for…  loop appropri-ately. For example, in a form that should be treated as two columns, the index values 0, 2,  4, … will access the left column and 1, 3, 5, … will access the right column. Document 5.8  gives an example.

Document 5.8 (sumForm2.htm)

<html>

<head>

<title>Sum a column of values</title>

</head>

<body>

<form name="sumform">

<table border>

<tr><td><input type="text" value="Value 1" /></td>

<td><input type="text" value="3.3" /></td></tr>

<tr><td><input type="text" value="Value 2" /></td>

<td><input type="text" value="3.9" /></td></tr>

<tr><td><input type="text" value="Value 3" /></td>

<td><input type="text" value="7.1" /></td></tr>

</table>

Here is the sum of all the values.<br />

<input type="text" name="sum" value="0"

/><br />

</form>

125 5.5 Using Arrays to Access the Contents of Forms

<script language="javascript" type="text/javascript">

var sum=0;

for (var i=1; i<(sumform.elements.length−1); i+=2) sum+=parseFloat(sumform.elements[i].value);

sumform.elements[sumform.elements.length−1].value=sum;

</script>

</body>

</html>

Document  5.8  sums  the  right  hand  column  of  values.  Although  the  output  from  Document 5.8 looks like a table that could be represented by a two-dimensional array, that  is not the case here. The input fields are still numbered consecutively, left to right and  top to bottom, in the order in which they appear. Also, remember that if the input fields  containing the values you wish to process don’t appear first inside the <form>…</form> 

tag, then the starting position will need to be offset appropriately.

5.5.2

Accessing type="radio" and type="checkbox" Fields Consider this fragment from an HTML document.

Employee is punctual:

Y <input type="radio" name="punctual" value="Y"

checked />&nbsp; &nbsp; &nbsp;

N <input type="radio" name="punctual" value="N"

/><br />

This code defines a type="radio" field with two possible values. If you look at the  elements array associated with the form containing this fragment, each field will be  stored as a separate element in the elements array. However, what you really want to  know  is  which  button  in  the "punctual" group  has  been  pressed.  Similarly, with  a  group  of type="checkbox"  fields,  you  want  to  know  which  choices  are  selected. 

Conveniently, each group of radio buttons or checkboxes is associated with its own array,  assigned the name you have provided for that group. Document 5.9 provides some exam-ples of how to use arrays to access the contents of radio buttons and checkboxes.

126 5 Using Arrays in HTML/JavaScript

5

Document 5.9 (buttonAccess.htm)

<html>

<head>

<title>Accessing Radio Buttons and Checkboxes</title>

</head>

<body>

Access contents of form fields...<br />

<form>

Give name: <input type="text" name="Ename" size="15"

value="Mr. Bland" /><br />

Employee is punctual:

Y <input type="radio" name="punctual" value="Y"

checked />&nbsp; &nbsp; &nbsp;

N <input type="radio" name="punctual" value="N" /><br />

Employee likes these animals:

Dogs <input type="checkbox" name="animals" value="dogs" />

Cats <input type="checkbox" name="animals" value="cats"

checked />

Boa constrictors <input type="checkbox" name="animals"

value="boas" checked /><br />

<input type="button"

value="Check here to examine form contents."

onclick="howMany.value=elements.length;

contents.value=elements[parseFloat(n.value)].value;

var i;

if (punctual[0].checked)

alert(Ename.value+' is always on time.');

elsealert(Ename.value+' is always late.');

for (i=0; i<animals.length; i++) {

if (animals[i].checked) alert(Ename.value+

' likes '+animals[i].value);

}; " /><br />

# elements: <input type="text" name="howMany"

value="0" /><br />

Which one (0 to # elements - 1)? <input type="text" name="n"

value="1" />

Contents: <input type="text" name="contents"

value="--" /><br />

</form>

</body>

</html>

127 5.5 Using Arrays to Access the Contents of Forms

The output shows the screen after the button box has been clicked and the first alert()  box is displayed.

5.5.3

Accessing Multiple Options Chosen in <select> Tags

Previously, the select tag used to create pull-down lists has allowed selecting only one  value from the list. However, it is also possible to select multiple values from a list of  options. Document 5.10 shows how to extract the value or values selected from a pull-down list, using the options array that is automatically generated for these lists.

Document 5.10 (chooseSelect.htm)

<html>

<head>

<title>Using values from a select list</title>

<script language="JavaScript" type="text/javascript" >

function whichSelected(list) { var n=list.length;

var i;

var s="";

for (i=0; i<n; i++) {

if (list.options[i].selected)

s = s+" "+list.options[i].value;

} return s;

</script>}

</head>

<body >

<h2>Shows how to access values from a <font face="courier">&lt;select&gt;</font> tag.</h2>

128 5 Using Arrays in HTML/JavaScript

5

<h3>Only one item can be chosen...</h3>

<select name="unique" size="3">

<option value="unique1" selected>unique 1</option>

<option value="unique2" >unique 2</option>

<option value="unique3" >unique 3</option>

</select><br />

Click in this field to see what you have chosen: <input type="text" name="selectedUnique"

onfocus="selectedUnique.value =

unique.options[unique.selectedIndex].value ;" /><br />

<h3>Multiple items can be chosen...</h3>

Hold down <font face="Arial"><b>Shift</b></font> or

<font face="Arial"><b>Ctrl</b></font>

key to make multiple selections.<br />

<select name="multiple" size="3" multiple>

<option value="multiple1" selected>multiple 1</option>

<option value="multiple2">multiple 2</option>

<option value="multiple3" >multiple 3</option>

</select><br />

Click on this field to see what you have chosen:

<input size="40" type="text" name="selectedMultiple"

onfocus="selectedMultiple.value=whichSelected(multiple);"

/><br />

</body>

</html>

In the first case illustrated in Document 5.10, where only one item can be selected, the  selected item in the unique list is obtained from the options array created by JavaScript,  using the selectedIndex property of the select element:

129 5.6 Hiding the Contents of a JavaScript Script

unique.options[unique.selectedIndex].value

The shaded line in Document 5.10 shows how to define a multiple-choice pull-down  list—by  including  the multiple  attribute.  These  choices  can  be  identified,  again  by  using the options array. In this case, a function has been written to accomplish this task,  using code like this to search through all the elements of options to find selected  elements:

if (list.options[i].selected)…

JavaScript functions will be discussed in detail in Chap. 6.

5.6

Dans le document Guide to HTML, JavaScript and PHP (Page 137-144)