检查数组中的值是否与Selectbox的值相同



是否可以检查内部和数组的值与选择框的值之间是否存在匹配?我需要检查选择框的值是否在数组中,因此,如果是,我可以将其类更改为其他。在这里noobie

编辑

我一直在努力重新创建我的问题,以便更容易理解。在这里的小提琴上,您可以看到我正在使用按钮动态创建新的SelectBox!在每个按钮中,选择都被禁用,但仅适用于该框。我要做的是,如果选择一个值,则所有其他值都将在所有其他框中停用。我尝试在选定值的所有值中插入所有值,然后对其进行比较以禁用它们,但我只能在每个框中进行操作。另外,Selectbox的数据来自数据库! 有任何提示吗?

//这应该禁用选定的选项

$('#sig_3').on('change', '.c_service_c', function() {
  var value = $('#type').val();
  var selected_array = [];
  $('.c_service_c option:selected').each(function() {
    if ($(this).val() != "") { //$(this).val()!="" --> remove the check on 'Please Select' option
      selected_array.push($(this).val()); //save selected values so that they can be disabled later
    }
  });
  console.log('print: ' + selected_array);
  //reset all values to 'Y' before changing selected to 'N'
  $('.c_service_c option').each(function() {
    if ($(this).val() != "") { //$(this).val()!="" --> remove the check on 'Please Select' option
      $('.c_service_c option').addClass('Y'); //resetting all options to 'Y'
    }
  });
  if (selected_array.indexOf($('.c_service_c').val()) > -1) {
    $('.c_service_c option:selected').removeClass('Y');
    $('.c_service_c option:selected').addClass('N');
    console.log('it prints here too');
  }
  //disable all selected values in options array
  if (selected_array.indexOf($(this).val()) > -1) {
    $('.c_service_c option:selected').removeClass('Y');
    $('.c_service_c option:selected').addClass('N');
    console.log('it prints');
  }
  //disable all selected values
  $('.c_service_c option').each(function() {
    if ($(this).val() != "") { //$(this).val()!="" --> remove the check on 'Please Select' option
      if ($(this).hasClass('N')) {
        $(this).attr("disabled", true);
        $(this).removeClass('N');
      } else {
        $(this).addClass('Y');
        $(this).attr("disabled", false);
      }
    }
  });
});

如果您需要每个 options检查 option是否存在数组中,并且基于禁用它,则只需要使用 .each()函数循环 options并执行您的操作检查。

这应该是您的代码:

$("#dropdown option").each(function() {
  if (arr.indexOf($(this).val()) > -1) {
    $(this).attr('disabled', true);
  }
});

var arr = ["apple", "tomato", "peach", "pineapple"];
$("#dropdown option").each(function() {
  if (arr.indexOf($(this).val()) > -1) {
    $(this).attr('disabled', true);
  }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<select id="dropdown">
  <option value="apple">Apples</option>
  <option value="orange">Oranges</option>
  <option value="pineapple">Pineapple</option>
  <option value="lemon">Lemon</option>
</select>

编辑:

i 更新了您的小提琴,因此它正在工作,您的问题是您在$().each()的本地范围中声明了selected_array,需要将其声明为全球。

假设您也使用jQuery,并假设我正确理解您的要求,请尝试以下内容:

//available values
var arr = ["apple", "tomato", "peach", "pineapple"];
$("#select-dropdown").on("change", function() {
  //store value on local variable
  var value = $(this).val();
  //search the value inside the array, return -1 if can't find
  var index = arr.indexOf(value);
  if (index != -1) {
    //found value in array, add the new class!
    $(this).addClass("my-new-class");
  } else {
    //didnt found anything, remove class
    $(this).removeClass("my-new-class");
  }
});
.my-new-class{
  background:green;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<select id="select-dropdown">
  <option value="apple">Apples</option>
  <option value="orange">Oranges</option>
  <option value="pineapple">Pineapple</option>
</select>

最新更新