javascript - 如何通过选择下拉值来生成字段?

标签 javascript php

我创建了下拉列表,其中包含实验室、药房、健身房、食品字段。当我从下拉列表中选择实验室时,它会生成新字段“名称”。 当我选择药房字段时,它必须显示上传按钮字段。请建议我如何执行此操作。

我的表单

   <form action="<?php echo $this->getFormAction(); ?>" id="quickservice" method="post">
 <div class="fieldset">
<ul class="form-list">
 <li>
            <label for="Servicetype" class="required"><em>*</em><?php echo $this->__('Service Type') ?></label>
                        <div class="input-box">
                            <select name="servicetype" class="input-text required-entry validate-select" style="width: 150px;" />
                                    <option value=""><?php echo $this->__('--Please Select--')?></option>
                                    <option value="Lab"><?php echo $this->__('Lab')?></option>
                                    <option value="Hospital"><?php echo $this->__('Hospital')?></option>
                                    <option value="Food"><?php echo $this->__('Food')?></option>
                                    <option value="Gym"><?php echo $this->__('Gym')?></option>
                                    <option value="Physio"><?php echo $this->__('Physio')?></option>
                                    <option value="Nurse"><?php echo $this->__('Nurse')?></option>
                            </select>
                        </div>

            </li>
</ul>
<div class="button">
    <p class="required"><?php echo $this->__('* Required Fields') ?></p>
    <button id= "submit"  type="submit" class="button" title="<?php echo $this->__('Submit') ?>" name="send" id="send2"><span><span><?php echo $this->__('Submit') ?></span></span></button>
</div>
  </div>
     </form>

最佳答案

如果你想在选择一个选项时显示/隐藏某些特定字段并且你不希望页面刷新,你应该使用 JavaScript 代码,例如如果你想在选择“实验室”值时显示上传字段你可以这样做:

<!doctype html>
<head>
    <script>
        function showHidden(elem){
           if(elem.value == 'Lab')
              document.getElementById('fileToUpload').style.display = "block";
            else
              document.getElementById('fileToUpload').style.display = "none";
        }
    </script>

</head>
<body>
<form action="<?php echo $this->getFormAction(); ?>" id="quickservice" method="post"  enctype="multipart/form-data">
    <div class="fieldset">
        <ul class="form-list">
            <li>
            <label for="Servicetype" class="required"><em>*</em><?php echo $this->__('Service Type') ?></label>
                <div class="input-box">
                    <select id='servicetype'   onchange="showHidden(this)" name="servicetype" class="input-text required-entry validate-select" style="width: 150px;" />
                            <option value=""><?php echo $this->__('--Please Select--')?></option>
                            <option value="Lab" id='lab'><?php echo $this->__('Lab')?></option>
                            <option value="Hospital"><?php echo $this->__('Hospital')?></option>
                            <option value="Food"><?php echo $this->__('Food')?></option>
                            <option value="Gym"><?php echo $this->__('Gym')?></option>
                            <option value="Physio"><?php echo $this->__('Physio')?></option>
                            <option value="Nurse"><?php echo $this->__('Nurse')?></option>
                    </select>
                </div>
            </li>
        </ul>
        <div id="fileToUpload" style='display: none;'>
            <input type="file" name="fileToUpload" />
        </div>
        <div class="button">
            <p class="required"><?php echo $this->__('* Required Fields') ?></p>
            <button id= "submit"  type="submit" class="button" title="<?php echo $this->__('Submit') ?>" name="send" id="send2"><span><span><?php echo $this->__('Submit') ?></span></span></button>
        </div>
    </div>
</form>
</body>

如果你想显示/隐藏更多字段,你可以使用相同的逻辑!

<!doctype html>
<head>
    <script>
        function showHidden(elem){
           if(elem.value == 'Lab')
           {
              document.getElementById('fileToUpload').style.display = "block";
              document.getElementById('city_name').style.display = "none";
           }
           else if(elem.value == 'Hospital')
           {
              document.getElementById('city_name').style.display = "block";
              document.getElementById('fileToUpload').style.display = "none";
           }
           else
           {
              document.getElementById('fileToUpload').style.display = "none";
              document.getElementById('city_name').style.display = "none";
           }
        }
    </script>

</head>
<body>
<form action="<?php echo $this->getFormAction(); ?>" id="quickservice" method="post"  enctype="multipart/form-data">
    <div class="fieldset">
        <ul class="form-list">
            <li>
            <label for="Servicetype" class="required"><em>*</em><?php echo $this->__('Service Type') ?></label>
                <div class="input-box">
                    <select id='servicetype'   onchange="showHidden(this)" name="servicetype" class="input-text required-entry validate-select" style="width: 150px;" />
                            <option value=""><?php echo $this->__('--Please Select--')?></option>
                            <option value="Lab" id='lab'><?php echo $this->__('Lab')?></option>
                            <option value="Hospital"><?php echo $this->__('Hospital')?></option>
                            <option value="Food"><?php echo $this->__('Food')?></option>
                            <option value="Gym"><?php echo $this->__('Gym')?></option>
                            <option value="Physio"><?php echo $this->__('Physio')?></option>
                            <option value="Nurse"><?php echo $this->__('Nurse')?></option>
                    </select>
                </div>
            </li>
        </ul>
        <div id="fileToUpload" style='display: none;'>
            <input type="file" name="fileToUpload" />
        </div>
        <div id="city_name" style='display: none;'>
            <input type="text" name="city" />
            <input type="text" name="name" />
        </div>
        <div class="button">
            <p class="required"><?php echo $this->__('* Required Fields') ?></p>
            <button id= "submit"  type="submit" class="button" title="<?php echo $this->__('Submit') ?>" name="send" id="send2"><span><span><?php echo $this->__('Submit') ?></span></span></button>
        </div>
    </div>
</form>
</body>

关于javascript - 如何通过选择下拉值来生成字段?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33754746/

相关文章:

javascript - 使用获取结果进行另一个获取请求(JS/React)

php - 是否有类似 &nbsp 的东西?

php - 'c :/wamp64/bin/php/php7. 0.10/ext/php_oci8_12c.dll' - %1 不是有效的 win32 应用程序

javascript - 不再支持 JSpec?

PHP 检查 URL 是否包含查询字符串

php - Laravel 5.8 验证错误返回到所有输入的特定 View ->旧的不工作

PHP 使用谷歌翻译翻译 HTML 页面

javascript - html 图像何时开始下载数据?

javascript - jquery 删除元素在 Sharepoint 内容编辑器中

javascript - Alfresco 调试器无法打开包含导入标记的 js