Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/75.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 检查数组中的值是否与选择框中的值相同?_Javascript_Jquery_Html_Arrays - Fatal编程技术网

Javascript 检查数组中的值是否与选择框中的值相同?

Javascript 检查数组中的值是否与选择框中的值相同?,javascript,jquery,html,arrays,Javascript,Jquery,Html,Arrays,是否可以检查和数组中的值与选择框的值是否匹配?我需要检查选择框的值是否在数组中,因此如果是,我可以将其类更改为其他类。这里的noobie 编辑 我一直在努力重新设计我的问题,以便更容易理解。 在这把小提琴上,你可以看到我正在使用一个按钮动态创建新的选择框!在每个按钮中,选择被禁用,但仅针对该框。我想做的是,如果选择了一个值,那么所有其他值都将在所有其他框中停用。 我尝试在数组中插入所选值的所有值,然后比较它们以禁用它们,但我只在每个框中进行了尝试。选择框中的数据也来自数据库! 有什么建议吗 //

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

编辑

我一直在努力重新设计我的问题,以便更容易理解。 在这把小提琴上,你可以看到我正在使用一个按钮动态创建新的选择框!在每个按钮中,选择被禁用,但仅针对该框。我想做的是,如果选择了一个值,那么所有其他值都将在所有其他框中停用。 我尝试在数组中插入所选值的所有值,然后比较它们以禁用它们,但我只在每个框中进行了尝试。选择框中的数据也来自数据库! 有什么建议吗

//这将禁用选定的选项

$('#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);
      }
    }
  });
});

假设您也在使用jQuery,并且假设我正确理解您的要求,请尝试以下操作:

//可用值 var arr=[苹果、番茄、桃子、菠萝]; $select-dropdown.onchange,函数{ //将值存储在局部变量上 var值=$this.val; //搜索数组中的值,如果找不到,则返回-1 var指数=arr.indexOfvalue; 如果索引!=-1{ //在数组中找到值,添加新类! $this.addClassmy-new-class; }否则{ //没有发现任何东西,请删除类 $this.removeClassmy-new-class; } }; .我的新课{ 背景:绿色; } 苹果 橘子 菠萝 如果要让每个选项检查该选项是否存在于数组中,并基于是否禁用该选项,则只需使用.each函数循环选项并执行检查

这是您的代码应该如何编写的:

$("#dropdown option").each(function() {
  if (arr.indexOf($(this).val()) > -1) {
    $(this).attr('disabled', true);
  }
});
var arr=[苹果、番茄、桃子、菠萝]; $dropdown option.eachfunction{ 如果arr.indexOf$this.val>-1{ $this.attr'disabled',true; } }; 苹果 橘子 菠萝 柠檬
您是要从选择框中检索选定的值,还是要检索所有值?我要交叉引用它们。。若要查看数组中的哪些项以禁用它们,请显示您的代码示例,以便我们可以提供相关答案以帮助您,并且我们可以使用相关代码演示该答案以显示其工作原理。请同时阅读和guidelines@DavidThomas我问是否有可能做这样的事情,我从来没有说过我写了一个代码我想做的是,它确实存在,以禁用它,所以如果我创建任何新的选择框,它将不可用,但禁用它们只在该框中,而不是在中others@noel293还有什么?你能根据相关情况编辑你的问题吗?@noel293好的,我会检查一下,但是你也能发布你的HTMl吗?@chdsk很好的例子伙伴,但是如果它们没有被选中,是否有可能让它们再次可用?这并不完全符合我想要做的,但我有它背后的逻辑!请查看我编辑的问题以了解更多详细信息