Show input field only if a specific option is selected

后端 未结 3 1275
北恋
北恋 2021-01-31 20:15

I\'m trying to make a form which checks if a certain option is selected from a \"select\" tag. Here is my current HTML:


                        
    
提交评论

  • 2021-01-31 21:03

    here you go:

    function yesnoCheck(that) {
        if (that.value == "other") {
      alert("check");
            document.getElementById("ifYes").style.display = "block";
        } else {
            document.getElementById("ifYes").style.display = "none";
        }
    }
    <select onchange="yesnoCheck(this);">
        <option value="">Valitse automerkkisi</option>
        <option value="lada">Lada</option>
        <option value="mosse">Mosse</option>
        <option value="volga">Volga</option>
        <option value="vartburg">Vartburg</option>
        <option value="other">Muu</option>
    </select>
    
    <div id="ifYes" style="display: none;">
        <label for="car">Muu, mikä?</label> <input type="text" id="car" name="car" /><br />
    </div>

    0 讨论(0)
  • 2021-01-31 21:13

    Check out: Display div if a specific select option value is selected

    Also don't use ID use data attributes or value to check comparison.

    0 讨论(0)
  • 提交回复
    热议问题