简体   繁体   English

jQuery:尝试遍历选择选项值

[英]jQuery: Trying to iterate through select options values

I'm trying to iterate through some select options using jQuery. 我正在尝试使用jQuery遍历一些选择选项。 In doing so, I'm trying to make sure that each of those has a selected option. 为此,我试图确保each选项都具有selected选项。 From those selected options, I want to travel up the DOM tree to the closest <tr> element and travel back down the DOM tree to find the <span> element of that row and change the style of a checkmark to represent the completion of that <select> form element. 从那些selected选项中,我想在DOM树中向上移动到closest <tr>元素,然后在DOM树中向下移动,以find该行的<span>元素,并更改选中标记的样式以表示该行的完成。 <select>表单元素。

On the <select> dropdown, I have an empty value as the default. <select>下拉列表中,我有一个空值作为默认值。 So I'd like to make sure there is something >= 0 selected. 因此,我想确保选择的>= 0

I'm still fairly new to jQuery and coding in general so I'm afraid there is something missing that I haven't learned yet. 我对jQuery和编码仍然相当陌生,因此恐怕我还没有学习到一些遗漏的东西。

application.js application.js

$('#selectId > option').each(function() {

  var formValue = $(this);

  if ( formValue.is(':selected') && formValue.val() >= 0 ) {
  formValue.closest('tr').find('span').removeClass('incomplete').addClass('complete');
  }

});

application.js application.js

$('form select option:selected[value!=""]').each(function(index, element) {      
   var span = $(element).closest('tr').find('td:first span');
   $(span).removeClass('incomplete').addClass('complete');
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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