how to activate a textbox if I select an other option in drop down box

后端 未结 6 2018
滥情空心
滥情空心 2020-12-23 18:13

suppose I\'ve a 3 options in a drop down box say red , blue, others. If a user select option as an others then below a text box should be visible to wrtie his own favour

相关标签:
6条回答
  • 2020-12-23 18:45

    Coded an example at http://jsbin.com/orisuv

    HTML

    <select name="color" onchange='checkvalue(this.value)'> 
        <option>pick a color</option>  
        <option value="red">RED</option>
        <option value="blue">BLUE</option>
        <option value="others">others</option>
    </select> 
    <input type="text" name="color" id="color" style='display:none'/>
    

    Javascript

    function checkvalue(val)
    {
        if(val==="others")
           document.getElementById('color').style.display='block';
        else
           document.getElementById('color').style.display='none'; 
    }
    
    0 讨论(0)
  • 2020-12-23 18:45

    Inline:

    <select 
    onchange="var val = this.options[this.selectedIndex].value;
    this.form.color[1].style.display=(val=='others')?'block':'none'">
    

    I used to do this

    In the head (give the select an ID):

    window.onload=function() {
      var sel = document.getElementById('color');
      sel.onchange=function() {
        var val = this.options[this.selectedIndex].value;
        if (val == 'others') {
          var newOption = prompt('Your own color','');
          if (newOption) {
            this.options[this.options.length-1].text = newOption;
            this.options[this.options.length-1].value = newOption;
            this.options[this.options.length] = new Option('other','other');
          }
        }
      }
    }
    
    0 讨论(0)
  • 2020-12-23 18:55

    As Umesh Patil answer have comment say that there is problem. I try to edit answer and get reject. And get suggest to post new answer. This code should solve problem they have (Shashi Roy, Gaven, John Higgins).

    <html> 
    <head>  
    <script type="text/javascript">
    function CheckColors(val){
     var element=document.getElementById('othercolor');
     if(val=='others')
       element.style.display='block';
     else  
       element.style.display='none';
    }
    
    </script> 
    </head>
    <body>
      <select name="color" onchange='CheckColors(this.value);'> 
        <option>pick a color</option>  
        <option value="red">RED</option>
        <option value="blue">BLUE</option>
        <option value="others">others</option>
      </select>
    <input type="text" name="othercolor" id="othercolor" style='display:none;'/>
    </body>
    </html>
    
    0 讨论(0)
  • 2020-12-23 18:55

    This will submit the right form response (i.e. Select value most of the time, and Input value when the Select box is set to "others"). Uses jQuery:

      $("select[name="color"]").change(function(){
        new_value = $(this).val();
        if (new_value == "others") {
          $('input[name="color"]').show();      
        } else {
          $('input[name="color"]').val(new_value);
          $('input[name="color"]').hide();
        }
      });
    
    0 讨论(0)
  • 2020-12-23 19:02

    Below is the core JavaScript you need to write:

    <html> 
    <head>  
    <script type="text/javascript">
    function CheckColors(val){
     var element=document.getElementById('color');
     if(val=='pick a color'||val=='others')
       element.style.display='block';
     else  
       element.style.display='none';
    }
    
    </script> 
    </head>
    <body>
      <select name="color" onchange='CheckColors(this.value);'> 
        <option>pick a color</option>  
        <option value="red">RED</option>
        <option value="blue">BLUE</option>
        <option value="others">others</option>
      </select>
    <input type="text" name="color" id="color" style='display:none;'/>
    </body>
    </html>
    
    0 讨论(0)
  • 2020-12-23 19:03

    Simply

    <select id = 'color2'
            name = 'color'
            onchange = "if ($('#color2').val() == 'others') {
                          $('#color').show();
                        } else {
                          $('#color').hide();
                        }">
      <option value="red">RED</option>
      <option value="blue">BLUE</option>
      <option value="others">others</option>
    </select>
    
    <input type = 'text'
           name = 'color'
           id = 'color' />
    

    edit: requires JQuery plugin

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