jQuery if in array but exclude some string

问题: I have six option values from database table similar to this: #blog and #hariciURL and #portfolio blah blah blah similar like this. I have another table have that values,...

问题:

I have six option values from database table similar to this: #blog and #hariciURL and #portfolio blah blah blah similar like this.

I have another table have that values, I check if same values in the array that option value will be disabled but except #hariciURL option value never disable.

How can I exclude #hariciURL not disabled from this option menu? please check codes you can understand what I mean.

sorry, my bad English.

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

回答1:

What you most likely want is

  1. since the seolinks seems to be a string, you need to split it so that it becomes an array.
  2. check that the BolumleriAl is not the same as the Exclude
    • if they are different then check if it is found in the seolinks array and if so then disable it
  3. you are already iterating over the option elements so no need to use a selector to find the element to disable. Just use 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)");
  }
});


回答2:

You are looping through the options, so you can just check if the option value, BolumleriAl, is not the excluded one.

Also, You can move the variables seolink and Exclude out of the loop, as these do change not every time.

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

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

  // if the option value is not excluded AND it is in the array
  if (BolumleriAl !== Exclude && $.inArray(seolink, BolumleriAl)) {
    $('#degeriAL option[value="' + seolink + '"]')
    .prop("disabled", true)
    .addClass("secimMenusuDisabled")
    .nextAll();
  }
});

回答3:

According to what I understoud from your question, I think your if condition was inverted.

Also, you should use the value given by $.each function

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

});

条评论

请先 登录 后评论
不写代码的码农
小编

篇文章

作家榜 »

  1. 小编 文章
返回顶部
部分文章转自于网络,若有侵权请联系我们删除