jQuery 如果在数组中但排除了一些字符串

jQuery if in array but exclude some string

我有来自数据库 table 的六个选项值与此类似:#blog 和 #hariciURL 和 #portfolio blah blah blah 类似于这样。

我有另一个 table 有那个值,我检查数组中是否有相同的值,该选项值将被禁用,但除了 #hariciURL 选项值从不禁用。

如何从该选项菜单中排除#hariciURL not disabled? 请检查代码你能明白我的意思。

对不起,我的英语不好。

    $().ready(function() {

        $('#degeriAL option').each(function() { // option menüsündeki tüm değerleri al
            //console.log(BolumleriAl);
            var BolumleriAl = $(this).val(); // tüm valuelerini bir değişkene ata
            

            var seolink = ("#services", "#hariciURL"); 
            
//var seolink = "<?php echo $BolumVarmiSorgula->seolink;?>"; // this original code come from database.
             console.log(seolink);
            var bolumisimleri = $('#degeriAL option[name]');
            
            var exclude = "#hariciURL"; //this will be never disabled


            if ($.inArray(seolink, BolumleriAl) && BolumleriAl !== exclude) { // iki dizi içinde eşleşen varmı diye bak

                $('#degeriAL option[value="' + seolink + '"]').prop("disabled", true).addClass("secimMenusuDisabled").addClass(".secimMenusuDisabled" + "(bölüm mevcut)").nextAll(); //option menüdeki dizi içinde olan tüm  değerlerini veritabanından gelenlerle karşılaştır ve eşleşenleri option menü içinden disabled yap
            }

        });

    });
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<select id="degeriAL" name="bolumLink" class="form-control form-control-sm" required="">
      <option value="" required="">Bölüm Seçiniz...</option>
      <option value="#featured-services"> Diğer Hizmetler </option>
      <option value="#about"> Hakkımızda </option>
      <option value="#services"> Hizmetler </option>
      <option value="#call-to-action"> Tıklama Eylemi </option>
      <option value="#blog"> Blog </option>
      <option value="#skills"> Yatay İstatistik Çizelgesi </option>
      <option value="#facts"> Rakamsal İstatistik </option>
      <option value="#portfolio"> Ürünler </option>
      <option value="#clients"> Referansların Logoları </option>
      <option value="#testimonials"> Müşteri Görüşleri </option>
      <option value="#team"> Bizim Takım &amp; Çalışanlar </option>
      <option value="#contact"> İletişim / Form / Harita </option>
      <option value="#hariciURL"> Harici Link </option>
    </select>

$('#degeriAL option').each(function() {
  var BolumleriAl = $(this).val();

  var seolink = "<?php echo $BolumVarmiSorgula->seolink;?>";
  var bolumisimleri = $('#degeriAL option[name]');

  var Exclude = "#hariciURL"; //this will be never disabled


  if (jQuery.inArray(seolink, BolumleriAl)) {

    $('#degeriAL option[value="' + seolink + '"]').prop("disabled", true).addClass("secimMenusuDisabled").addClass(".secimMenusuDisabled" + "(that already exist, you can not add more than one)").nextAll();
  }
});

您正在遍历选项,因此您可以只检查选项值 BolumleriAl 是否不是被排除的值。

此外,您可以将变量 seolink 和 Exclude 移出循环,因为它们并非每次都会更改。

var seolink = "https://example.com";
var Exclude = "#hariciURL"; //this will be never disabled
var Bölümler = ['test3', 'test7'];

$('#degereriAL').children('option').each(function() {
  var currentValue = $(this).val();

  if (currentValue == Exclude || $.inArray(currentValue, Bölümler) > -1) {
    // The value is excluded (#hariciURL)
    // OR the value is in the Bölümler array
  } else {
    $(this)
      .prop("disabled", true)
      .addClass("secimMenusuDisabled");
  }
});
select {
  min-width: 10em;
}

select option[disabled] {
  color: #eee;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<select id="degereriAL" size="8">
  <option value="https://example.com">Test</option>
  <option value="#hariciURL">Test 2</option>
  <option value="test3">Test 3</option>
  <option value="test4">Test 4</option>
  <option value="test5">Test 5</option>
  <option value="test6">Test 6</option>
  <option value="test7">Test 7</option>
  <option value="test8">Test 8</option>
</select>

您最可能想要的是

  1. 由于 seolinks 似乎是一个字符串,您需要 split 使其成为一个数组。
  2. 检查 BolumleriAl 是否与 Exclude 相同
    • 如果它们不同,则检查是否在 seolinks 数组中找到它,如果是,则禁用它
  3. 您已经在遍历 option 元素,因此无需使用选择器来查找要禁用的元素。只需使用 this.

$('#degeriAL option').each(function() {
  var BolumleriAl = $(this).val();

  var seolink = "<?php echo $BolumVarmiSorgula->seolink;?>".split(' ');

  var Exclude = "#hariciURL"; //this will be never disabled

  if (BolumleriAl !== Exclude && $.inArray(BolumleriAl, seolink) > -1) {
    $(this).prop("disabled", true)
      .addClass("secimMenusuDisabled")
      //.addClass(".secimMenusuDisabled" + "(that already exist, you can not add more than one)");
  }
});

根据我从你的问题中了解到的情况,我认为你的if条件倒置了。

另外,你应该使用 $.each 函数给出的值

var seolink = "<?php echo $BolumVarmiSorgula->seolink;?>";
var Exclude = "#hariciURL"; //this will be never disabled

$('#degeriAL option').each(function(i,v) {

  Exclude = v != Exclude && $.inArray(seolink, v) ?  $('#degeriAL option[value="' + seolink + '"]').prop("disabled", true).addClass("secimMenusuDisabled").text('that already exist, you can not add more than one') : "#hariciURL";

});