Show and hide html element on selected option change

前端 未结 10 1601
耶瑟儿~
耶瑟儿~ 2021-01-12 04:31

In a JSP page, I have a dropdown list. When the first element of the list is selected, I want a text area to show right on click. I\'m new to Javascript/Jquery, so I\'m obvi

相关标签:
10条回答
  • 2021-01-12 04:55
    var drpVal=  $('#show').val();
    if( drpVal == "1")
    {
         $('#text_area').show(); 
              // or u can use
           $("#text_area").css("display", "");
     }
     else{
      $('#text_area').hide(); 
              // or u can use
           $("#text_area").css("display", "none");
     }
    
    0 讨论(0)
  • 2021-01-12 04:57

    Try this code:

    // This will create an event listener for the change action on the element with ID 'show'
    $('#show').change(function() {
    
         // If checkbox is 'checked'
        if($(this).is(':checked')) {
            // show the element that has the id 'txt_area' 
            $('#text_area').show();
        } else {
            // hide it when not checked
            $('#text_area').hide();
        }
    });
    
    0 讨论(0)
  • 2021-01-12 04:59

    You can use jQuery as following

    <script> function change() {
        var selectBox = document.getElementById("show");
        var selected = selectBox.options[selectBox.selectedIndex].value;
    
        if(selected === '1'){
            $('#text_area').show();
        }
        else{
            $('#text_area').hide();
        }
    }</script>
    
    0 讨论(0)
  • 2021-01-12 05:00

    You are getting the html element by document.getElementById that returns normal javascript object. Jquery methods hide() and show() are available for jquery objects only.

    But whatever you want to achieve can be achieved by simple Javascript, just made some simple changes.

    instead of show() and hide() use respectively textarea.style.display = "block" and textarea.style.display = "none";

    and remove the ); in the end of your code.

    use the fiddle link for working example. fiddle link

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