开发者

jQuery select index of dropdownlist based upon number of elements and value of current selection

开发者 https://www.devze.com 2022-12-27 16:26 出处:网络
I have a form with multiple dropdownlists on it.Some of them possess multiple values, others posess a blank choice and another random choice.

I have a form with multiple dropdownlists on it. Some of them possess multiple values, others posess a blank choice and another random choice.

Using jQuery how do I detect which dropdowns have the blank choice selected AND have only one other choice THEN select that other choice.

I got the selector开发者_StackOverflow中文版 part down,

$("[id*='_ddl']").each(function() {

but am not sure how to detect the number of choices available, and if the currently selected value is a blank.

Thanks!


$('select[id*=_ddl]').each(function() {
    var itemCount = $('option', this).length;
    var selectedText = $('option:selected', this).text();
    if (selectedText.length === 0 && itemCount === 2) {
        // do stuff
    }
});
0

精彩评论

暂无评论...
验证码 换一张
取 消