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

时间:2019-01-08 15:46:28

标签: jquery html5 option

我从数据库表中有六个与此类似的选项值:#blog和#hariciURL和#portfolio等等等等。

我有另一个表具有该值,我检查数组中是否禁用了该选项值的相同值,但#hariciURL选项值从未禁用。

如何从该选项菜单中排除未禁用的#hariciURL? 请检查代码,您可以理解我的意思。

对不起,我的英语不好。

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

3 个答案:

答案 0 :(得分:1)

您最可能想要的是

  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)");
  }
});

答案 1 :(得分:0)

您正在遍历选项,因此您只需检查选项值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>

答案 2 :(得分:0)

根据我从您的问题中了解的内容,我认为您的条件是否可以逆转。

此外,您应该使用$ .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";

});