439896 Simple JavaScript Function - Form Show/Hide Fields
N/A
Đang triển khai
Đã đăng vào hơn 13 năm trước
N/A
Thanh toán khi bàn giao
I need a JavaScript function to show/hide multiple form fields based on a drop-down list selection.
The select box options are populated by PHP so I need to be able to select the divs to show in the JavaScript code.
Example of form:
<select id="country" name"country" onchange="Change(this)">
<option>Please Select</option>
<?php
foreach ($option as $k) {
echo '<option value="'.$k.'">'.$k.'</option>';
}
?>
</select>
<div id="1" style="display:none">
<label>Name:</label>
<input type="text" name="name" />
</div>
<div id="2" style="display:none">
<label>Email:</label>
<input type="text" name="email" />
</div>
<div id="3" style="display:none">
<label>City:</label>
<input type="text" name="city" />
</div>
The JavaScript code should allow me to select for each option, something like;
if option value="" then show()
if option value="USA" then show(1,2,3)
if option value="Canada" then show(2)
if option value="UK" then show(3)
... and so on
The numbers inside of () are the div ID's to show.
NOTE: You will not be able to modify or change the values of the select list since it is populated from PHP.
Also, I need to be able to select "pre-selected" option on page load. Like on page load, load option value UK. This is for when I edit the information the fields show up for that option. I can use PHP to call the value also since it will be a different page. So if could be something like onpageload->select="<?php echo $country; ?>"
Please PM Questions or Demos.