【问题标题】:jQuery check how many select elements have selectedIndex !== 0jQuery 检查有多少选择元素 selectedIndex !== 0
【发布时间】:2018-08-10 11:36:07
【问题描述】:

我的页面中有多个选择元素,我需要找出有多少元素确实具有 selectedIndex !== 0

<select name="startMonth" class="selCalendar">
 <option value="">select</option>
 <option value="1" selected>Opt1</option>
 <option value="2">Opt2</option>
 <option value="4">Opt3</option>
</select>

<select name="startYear" class="selCalendar">
 <option value="">select</option>
 <option value="1">Opt1</option>
 <option value="2">Opt2</option>
 <option value="3">Opt3</option>
 </select>

<select name="endMonth" class="selCalendar">
 <option value="">select</option>
 <option value="1">Opt1</option>
 <option value="2">Opt2</option>
 <option value="3" selected>Opt3</option>
</select>

<select name="endYear" class="selCalendar">
 <option value="">select</option>
 <option value="1">Opt1</option>
 <option value="2">Opt2</option>
 <option value="3">Opt3</option>
</select>

我在更改时尝试了过滤器和 localStorage 对象,但没有成功.... 任何帮助,将不胜感激。谢谢!

【问题讨论】:

  • 您可以添加您尝试过的方法以及为什么不起作用?

标签: jquery select selector options selectedindex


【解决方案1】:

你可以这样做:

var savedArray = new Array({ name: "startMonth", value: 1 }, { name: "startYear", value: 0 }, { name: "endMonth", value: 3 }, { name: "endYear", value: 0 });

$('#startMonth, #startYear, #endMonth, #endYear').change((ev) => {
    var index = savedArray.findIndex((elem) => { return elem.name == ev.target.id });
    savedArray[index].value = ev.target.val();
});

const countZero = () => {
  let count = 0;
  savedArray.forEach((elem) => {
      count += elem.value == 0 ? 1 : 0;
  });
  return count;
};

我没有尝试上面的代码。要使其工作,您只需将 ID 添加到您的选择中。

【讨论】:

  • # 用于 Id 而不是用于名称.. 为所有选择框使用公共类
  • 没有 id 我们可以得到ev.target.id 和编辑savedArray 尽可能多。
【解决方案2】:

为什么要把事情复杂化,只需循环获取空值。

var $selects = $('select');
var nonEmptyValCount = 0;

$selects.each( function (index, el) {
    if (el.value !== '') nonEmptyValCount ++;
})

//output
console.log(nonEmptyValCount );

【讨论】:

  • 我之前也在考虑这个方向,虽然它会起作用,但我最喜欢 Freedomn-m 的过滤器解决方案,因为它非常聪明且简短。
【解决方案3】:

您可以直接在 jquery 选择器中执行此操作:

$(".selCalendar option[value!='']:selected")

例子:

$("#click").click(function() {

  var chosen = $(".selCalendar option[value!='']:selected");

  console.log(chosen.length)
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select name="startMonth" class="selCalendar">
 <option value="">select</option>
 <option value="1" selected>Opt1</option>
 <option value="2">Opt2</option>
 <option value="4">Opt3</option>
</select>

<select name="startYear" class="selCalendar">
 <option value="">select</option>
 <option value="1">Opt1</option>
 <option value="2">Opt2</option>
 <option value="3">Opt3</option>
 </select>

<select name="endMonth" class="selCalendar">
 <option value="">select</option>
 <option value="1">Opt1</option>
 <option value="2">Opt2</option>
 <option value="3" selected>Opt3</option>
</select>

<select name="endYear" class="selCalendar">
 <option value="">select</option>
 <option value="1">Opt1</option>
 <option value="2">Opt2</option>
 <option value="3">Opt3</option>
</select>

<button type='button' id='click'>count</button>

或者您可以使用filter 对选择进行回调:

$(".selCalendar").filter(function(i, e) {
    return $(e).val() !== "";
});

$("#click").click(function() {
  var chosen = $(".selCalendar").filter(function(i, e) {
    return $(e).val() !== "";
  });

  console.log(chosen.length)
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select name="startMonth" class="selCalendar">
 <option value="">select</option>
 <option value="1" selected>Opt1</option>
 <option value="2">Opt2</option>
 <option value="4">Opt3</option>
</select>

<select name="startYear" class="selCalendar">
 <option value="">select</option>
 <option value="1">Opt1</option>
 <option value="2">Opt2</option>
 <option value="3">Opt3</option>
 </select>

<select name="endMonth" class="selCalendar">
 <option value="">select</option>
 <option value="1">Opt1</option>
 <option value="2">Opt2</option>
 <option value="3" selected>Opt3</option>
</select>

<select name="endYear" class="selCalendar">
 <option value="">select</option>
 <option value="1">Opt1</option>
 <option value="2">Opt2</option>
 <option value="3">Opt3</option>
</select>

<button type='button' id='click'>count</button>

【讨论】:

  • 我这里没有可以点击的按钮,我会分析select变化的长度。所以你的第一个样品并不完美。但我能够使用你的过滤器样本,它既短又聪明。感谢您花时间在这方面,真的很有帮助。
  • 该按钮仅用于提供可重复使用的工作 sn-p(即,当您更改选择值时,您可以看到它是如何受到影响的)。可以改用 .change。
猜你喜欢
  • 1970-01-01
  • 2013-05-20
  • 2014-10-15
  • 1970-01-01
  • 2019-08-20
  • 1970-01-01
  • 2011-11-30
  • 1970-01-01
  • 2011-10-26
相关资源
最近更新 更多