display dropdown values based on previous dropdown

前端 未结 2 1427
傲寒
傲寒 2020-12-06 15:50

I have the following form fields:

        
        
    
    
    

    

Travelers: store the parent for each option, for example in a data-xyz attribute.



The jQuery:

$('#carrier_sold').change(function() {
   var parent = $(this).val();
   $('#payment_plan').children().each(function() {
      if($(this).data('parent') != parent) {
                $(this).hide();
      } else    $(this).show();
   });
});

Repeat for the next instance. Ideally, I'd make a class-based jQuery function that works off of classes instead of IDs, and pulls the respective children from each parent via a data-child attribute or similar.

And to jazz it up even more, you could hide the child unless the parent has a value. You can also experiment with change() vs. blur().

提交回复
热议问题