1

我正在尝试隐藏和显示几个选择和文本字段

这个想法是,在选择“Banco”时的第一个字段中,它会显示一个下拉列表供用户选择哪个银行,直到该部分一切正常,当在下一个“banco”下拉列表中人们选择“otros”时问题就来了它应该为客户显示一个文本字段来写银行的名称,但我已经做了所有事情,似乎无法让它工作

你能帮我吗??

这是JS

function formapago() {
if (document.getElementById('infopago').value == 'banco') {
    document.getElementById('banco').style.display = '';
    document.getElementById('tipopago').style.display='';
    }   

else {
          document.getElementById('banco').style.display = 'none';
          document.getElementById('tipopago').style.display='none';
}

if (document.getElementById('infopago').value == 'mp') {
    document.getElementById('mpcobro').style.display = '';
} else {
          document.getElementById('mpcobro').style.display = 'none';
}  
if (document.getElementById('banco').value == 'otros') {
    document.getElementById('otrosban').style.display = '';
    }   

else {
          document.getElementById('otrosban').style.display = 'none';
}   

}

和 HTLM

<select name="infopago" id="infopago" onchange='formapago()'>
          <option value="elegir" selected="selected">Elegir</option>
          <option value="banco">Banco</option>
          <option value="mp">Mercado Pago</option>
        </select>
        </p>
        <div id="banco" style="display: none">
      <p><strong>BANCO: </strong></p>
      <p>
        <label for="banco">Banco Emisor</label>
        <select name="banco" id="banco" onchange='formapago()'>
          <option value="elegir" selected="selected">Elegir</option>
          <option value="provincial">Provincial</option>
          <option value="mercantil">Mercantil</option>
          <option value="banesco">Banesco</option>
          <option value="venezuela">Venezuela</option>
          <option value="otros">Otro</option>
        </select>
      </p> </div>

      <div id="otroban" style="display: none">
      <p>
        <label for="otroban">Otro Banco</label>
        <input type="text" name="otroban" id="otroban"/>
      </p>
      </div> 
4

1 回答 1

2

Your problem is that you have two element with same ID.

This part :

  <div id="banco" style="display: none">
    <p><strong>BANCO: </strong></p>
    <p>
      <label for="banco">Banco Emisor</label>
      <select name="banco" id="banco" onchange='formapago()'>
       <option value="elegir" selected="selected">Elegir</option>
       <option value="provincial">Provincial</option>
       <option value="mercantil">Mercantil</option>
       <option value="banesco">Banesco</option>
       <option value="venezuela">Venezuela</option>
       <option value="otros">Otro</option>
       </select>
    </p> 
 </div>

div and select element have id="banco". Try to change select id.

EDIT

Here some problem i found when i see your HTML :

  1. Multiple element with same id.
  2. In your javascript, it try to get tipopago and mpcobro element, where is the element ?
  3. I think this part :
  document.getElementById('otrosban').style.display = '';

Should be :

  document.getElementById('otroban').style.display = '';
于 2013-02-05T06:50:15.343 回答