简体   繁体   中英

Jquery selected option text contains

Trying to make if condition of selected option of another select not the current which contains certain text string as a text and not the value.

Example:

<select class="Selected">
<option>Hello</option>
<option>Bye</option>
</select>

In the following case what I have is the fowling. a trigger of another select:

$(".Selected_Two").change(function () {
    if ($(this).closest('.div-block').find('.Selected:contains("Bye")')) {              
    alert('Bye');
    }
});

So basically if the select option is selected in the select when triggering the other select will have to see alert "Bye" But it is not working at this state. Any help is welcome.

Two ways to do that indexOf() for value or :contains for option text

indexOf()

$(".Selected_Two").change(function () {
    var SelectTwoValue = $(this).val();
    if ($(this).closest('.div-block').find('.Selected)').val().indexOf(SelectTwoValue ) > -1) {              
        alert('Bye');
    }
});

:contains and length

$(".Selected_Two").change(function () {
    var SelectTwoValue = $(this).val();
    if ($(this).closest('.div-block').find('.Selected option:selected:contains("'+SelectTwoValue+'")').length > 0) {              
       alert('Bye');
    }
});

Maybe you'll need to work around .val().indexOf() and :contains("'+SelectTwoValue+'")').length .. but I think you got the point

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM