gpt4 book ai didi

Jquery检查所有选择元素selectedIndex是否为0

转载 作者:行者123 更新时间:2023-12-01 04:04:49 25 4
gpt4 key购买 nike

我有一个日历范围的 4 个选择元素,我想检查使用选择器选择的所有选择元素是否都选择了第一个选项(selectedIndex == 0)。我什至可以检查所有它们的值是否不是“”,但似乎没有任何效果。

<select name="startMonth" class="selCalendar">
<option value="">Month</option>
<option value="1">January</option>
<option value="2">February</option>
...
<option value="12">December</option>
</select>

<select name="startYear" class="selCalendar">
<option value="">Year</option>
<option value="1950">1950</option>
<option value="1951">1951</option
....
<option value="2015">2015</option
</select>

<select name="endMonth" class="selCalendar">
<option value="">Month</option>
<option value="1">January</option>
<option value="2">February</option>
...
<option value="12">December</option>
</select>

<select name="endYear" class="selCalendar">
<option value="">Year</option>
<option value="1950">1950</option>
<option value="1951">1951</option
....
<option value="2015">2015</option
</select>

因此,我只想在所有条件都具有 selectedIndex == 0 或值为“”时进入我的 if 语句。

我尝试过的一些事情

if ( $( ".selCalendar" ).filter('option[value=""]').length )
{
...
}

if ( $(".selCalendar option[value='']" ).attr('selected', 'selected').length )
{
...
}

if( $( ".selCalendar option:selected" ).index() > 0)
{
...
}

if ( $(".selCalendar option:selected").length)
{
...
}

我在选择第一个选项的单个选择上找到了答案,但没有检查所有选择。

如有任何帮助,我们将不胜感激。谢谢

最佳答案

这是一个可能的解决方案:

<select name="startMonth" class="selCalendar">
<option value="">Month</option>
<option value="1">January</option>
<option value="2">February</option>
<option value="12">December</option>
</select>

<select name="startYear" class="selCalendar">
<option value="">Year</option>
<option value="1950">1950</option>
<option value="1951">1951</option>
<option value="2015">2015</option>
</select>

<select name="endMonth" class="selCalendar">
<option value="">Month</option>
<option value="1">January</option>
<option value="2">February</option>
<option value="12">December</option>
</select>

<select name="endYear" class="selCalendar">
<option value="">Year</option>
<option value="1950">1950</option>
<option value="1951">1951</option>
<option value="2015">2015</option>
</select>

<input type="button" value="Check" id="myButton" />

javascript

$("#myButton").click(function() {
var isAllBlank = true;
$(".selCalendar").each(function() {
if($(this).val() !== "") {
isAllBlank = false;
}
});

alert(isAllBlank);
});

<强> JSFiddle Demo

关于Jquery检查所有选择元素selectedIndex是否为0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30903985/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com