Domanda

<script type="text/javascript">
    function myfunction() {
        if (document.getElementById('myform').value == "yes") {
            document.getElementById('yesinput').style.display = '';
        } else {
            document.getElementById('yesinput').style.display = 'none';
        }
    }
</script>

<select name="myform" id="myform" onchange="myfunction()">
<option selected="selected">please select</option>
  <option value="yes">Yes</option>
  <option value="no">No</option>
</select>

<div id="yesinput" style="display:none">
<input name="input" type="text" />
</div>

<div id="noinput" style="display:none">
<input name="input" type="text" />
</div>

Guida ya. Come rendere se selezioniamo No avrà un altro campo di inserimento al di sotto del id = "noinput".

Ora sta funzionando se selezioniamo . fatemi sapere

È stato utile?

Soluzione

function myfunction() {
    if (document.getElementById('myform').selectedIndex == 0) {
        document.getElementById('noinput').style.display = 'none';
        document.getElementById('yesinput').style.display = 'inline';
    } else {
        document.getElementById('noinput').style.display = 'inline';
        document.getElementById('yesinput').style.display = 'none';
    }
}

Altri suggerimenti

È possibile costruire un nuovo elemento e aggiungerlo al DOM tramite Javascript. Un esempio:

var newInput = document.createElement("input");
newInput.setAttribute("id", "newElement");
newInput.setAttribute("type", "text");
document.body.appendChild(newInput);
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top