执行功能时输入不出现

时间:2017-09-12 12:00:13

标签: javascript php

我选择" otros"单击一个选择,将出现一个输入。为此,我已经使这个脚本工作不正常(输入没有出现)。

HTML / PHP的

<select onclick='atencion_otros()' id="atencion" name="atencion">
    <option selected>Atención</option>
    <option>Otros</option>
    <?php if($copy){ echo '<option selected>' . $atencion . '</option>';
    $result = mysqli_query($con,"SELECT * FROM `contactos_pmt` ORDER BY nombre_contacto");
    while($row = mysqli_fetch_array($result)){
     ?> <option> <?php echo $row['nombre_contacto']; ?> </option>
     <br><br><?php
     }
    }
    if($cargar_cliente){
    $result = mysqli_query($con,"SELECT * FROM `contactos_pmt` WHERE 
    nombre_empresa = '$nombre_cliente' ORDER BY nombre_contacto");
    while($row = mysqli_fetch_array($result)){
     ?> <option selected> <?php echo $row['nombre_contacto']; ?> </option>
     <br><br><?php
     }
    }
    ?>
</select>
<input onkeypress="return event.keyCode != 13;" style="display:none;" id="atencion_otros" name="atencion_otros" placeholder="Atención" />

的Javascript

<script type="text/javascript">
  function atencion_otros() {
    if (document.getElementById('atencion').value == 'Otros') {
        document.getElementById('atencion_otros').style.display = 'block';
    } else {
        document.getElementById('atencion_otros').style.display = 'none';
    }
  }
</script>

您是否知道为什么输入没有出现?

1 个答案:

答案 0 :(得分:2)

首先在所有<option>中添加值属性。

然后添加事件监听器。

document.addEventListener('DOMContentLoaded',function() {
    document.getElementById('atencion').onchange=atencion_otros;
},false);

现在你的功能

function atencion_otros() {
    if (document.getElementById('atencion').value == 'Otros') {
        document.getElementById('atencion_otros').style.display = 'block';
    } else {
        document.getElementById('atencion_otros').style.display = 'none';
    }
  }

现在从<select>删除onclick属性。