jQuery multiple select show/hide

前端 未结 5 1471
青春惊慌失措
青春惊慌失措 2021-01-20 05:39

I have a table with language translation. I\'m trying to create a script that will hide multiple language columns. The problem is that it only hide one field at a time. I ca

相关标签:
5条回答
  • 2021-01-20 06:12
    $(document).ready(function () {
        $( ".hide" ).change(function() {
            var value = $( this ).val();
            $('[class^=cell]').hide();
            $( ".cell" + value ).show();        
        })
    });
    

    Fiddle

    0 讨论(0)
  • 2021-01-20 06:13

    Problem is in code $(".cell" + str) after each block, move your code to hide in each section.

    $(".hide").change(function () {
        $('[class^=cell]').show();
        $(".hide option:selected").each(function () {
            $(".cell" + $(this).val()).hide();
        });
    }).trigger("change");
    

    DEMO

    0 讨论(0)
  • 2021-01-20 06:23

    Replace your JS with this:

    $(document).ready(function () {
       $( ".hide" ).change(function() {
       var str = [];
       $('[class^=cell]').show();
       $( ".hide option:selected" ).each(function() {
          var vq = $( this ).val();
          $('.cell'+vq).hide();    
       });
    })
    .trigger( "change" );
    });
    
    0 讨论(0)
  • 2021-01-20 06:30

    You need to hide every selected option while iterating loop like below :

    $( ".hide" ).change(function() {
        var str = "";
        $('[class^=cell]').show();
       $( ".hide option:selected" ).each(function() {
           str = $( this ).val();
           $( ".cell" + str ).hide();
        });
    }).trigger( "change" );
    

    Working Demo

    0 讨论(0)
  • 2021-01-20 06:38
    $(document).ready(function () {
    $(".hide" ).change(function() {
    var str = [];
    $(".hide option:selected").each(function() {
    str.push($( this ).val());
    });
    $('[class^=cell]').show();
    for(i=0; i<str.length; i++){
     $( ".cell" + str[i] ).hide();
    }
    }).trigger( "change" );
    });
    

    Demo:

    http://jsfiddle.net/LAZeX/

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