jQuery select change show/hide div event

前端 未结 9 1436
失恋的感觉
失恋的感觉 2020-11-27 17:34

I am trying to create a form which when the select element \'parcel\' is selected it will show a div but when it is not selected I would like to hide the div. Here is my mar

相关标签:
9条回答
  • 2020-11-27 18:06

    Try:

    if($("option[value='parcel']").is(":checked"))
       $('#row_dim').show();
    

    Or even:

    $(function() {
        $('#type').change(function(){
            $('#row_dim')[ ($("option[value='parcel']").is(":checked"))? "show" : "hide" ]();  
        });
    });
    

    JSFiddle: http://jsfiddle.net/3w5kD/

    0 讨论(0)
  • 2020-11-27 18:12

    Try this:

    $(function() {
        $("#type").change(function() {
            if ($(this).val() === 'parcel') $("#row_dim").show();
            else $("#row_dim").hide();
        }
    }
    
    0 讨论(0)
  • 2020-11-27 18:13

    Use following JQuery. Demo

    $(function() {
        $('#row_dim').hide(); 
        $('#type').change(function(){
            if($('#type').val() == 'parcel') {
                $('#row_dim').show(); 
            } else {
                $('#row_dim').hide(); 
            } 
        });
    });
    
    0 讨论(0)
  • 2020-11-27 18:18

    Try the below JS

    $(function() {
        $('#type').change(function(){
            $('#row_dim').hide(); 
            if ($(this).val() == 'parcel')
            {
                 $('#row_dim').show();
            }
        });
    });
    
    0 讨论(0)
  • 2020-11-27 18:19

    change your jquery method to

    $(function () { /* DOM ready */
        $("#type").change(function () {
            alert('The option with value ' + $(this).val());
            //hide the element you want to hide here with
            //("id").attr("display","block"); // to show
            //("id").attr("display","none"); // to hide
        });
    });
    
    0 讨论(0)
  • 2020-11-27 18:20

    Try this:

     $(function () {
         $('#row_dim').hide(); // this line you can avoid by adding #row_dim{display:none;} in your CSS
         $('#type').change(function () {
             $('#row_dim').hide();
             if (this.options[this.selectedIndex].value == 'parcel') {
                 $('#row_dim').show();
             }
         });
     });
    

    Demo here

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