Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
188 views
in Technique[技术] by (71.8m points)

javascript - .val() equals multiple values

I have the following (simple) code that shows/hides elements when a certain value is selected in a <select> tag.

Now I need it to add the class if .val() equals to not only '16' but also '14'.

    jQuery(document).ready(function( $ ){
    var Privileges = jQuery('.regeling');
var select = this.value;
Privileges.change(function () {
    if ($(this).val() == '16') {
        $('.hide_on_call').css("display", "inline-block");
        $('.hide_on_call_text').css("display", "block");
    }
    
    else $('.hide_on_call,.hide_on_call_text').css("display", "none");
    
});
});
question from:https://stackoverflow.com/questions/65906878/val-equals-multiple-values

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Simply add the second condition to the if using ||.

jQuery(document).ready(function( $ ){
    var Privileges = jQuery('.regeling');
    var select = this.value;
    Privileges.change(function () {
        if ($(this).val() == '16' || $(this).val() == '14' ) {
            $('.hide_on_call').css("display", "inline-block");
            $('.hide_on_call_text').css("display", "block");
        }
    
        else $('.hide_on_call,.hide_on_call_text').css("display", "none");
    
    });
});

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...