Как я могу удалить опции с тем же текстом из выбранного элемента?

beforeShowDate не работал для меня, поэтому я пошел вперед и разработал свое собственное решение:

$('#embeded_calendar').datepicker({
               minDate: date,
                localToday:datePlusOne,
               changeDate: true,
               changeMonth: true,
               changeYear: true,
               yearRange: "-120:+1",
               onSelect: function(selectedDateFormatted){

                     var selectedDate = $("#embeded_calendar").datepicker('getDate');

                    deactivateDates(selectedDate);
                   }

           });


              var excludedDates = [ "10-20-2017","10-21-2016", "11-21-2016"];

              deactivateDates(new Date());

            function deactivateDates(selectedDate){
                setTimeout(function(){ 
                      var thisMonthExcludedDates = thisMonthDates(selectedDate);
                      thisMonthExcludedDates = getDaysfromDate(thisMonthExcludedDates);
                       var excludedTDs = page.find('td[data-handler="selectDay"]').filter(function(){
                           return $.inArray( $(this).text(), thisMonthExcludedDates) >= 0
                       });

                       excludedTDs.unbind('click').addClass('ui-datepicker-unselectable');
                   }, 10);
            }

            function thisMonthDates(date){
              return $.grep( excludedDates, function( n){
                var dateParts = n.split("-");
                return dateParts[0] == date.getMonth() + 1  && dateParts[2] == date.getYear() + 1900;
            });
            }

            function getDaysfromDate(datesArray){
                  return  $.map( datesArray, function( n){
                    return n.split("-")[1]; 
                });
             }
1
задан Andy E 21 July 2010 в 15:20
поделиться

2 ответа

var remove = [], values = {}, value, i;
var options = document.getElementById('tur').getElementsByTagName('option');
for (i=0; i<options.length; i++) {
  value = options[i].innerHTML.replace(/^\s*|\s*$/g, '');
  if (value in values) remove.push(options[i]);
  else values[value] = true;
}

for (i=0; i<remove.length; i++) {
  remove[i].parentNode.removeChild(remove[i]);
}
0
ответ дан 2 September 2019 в 22:52
поделиться

Вы можете перебирать элементы , проверяя каждый из них на то, находится ли его текстовое содержимое в массиве. Если да, удалите элемент . Если нет, добавьте его содержимое в массив. Это позволит удалить лишние опции в списке.

Попробуйте: http://jsfiddle.net/FXq8W/

​var array = [];

​$('#tur option').each(function() {
    var $th = $(this);
    var text = $th.text();
    if( $.inArray(text, array) > -1 ) {
        $th.remove();
    } else {
        array.push( text );
    }
});​​​​​​​​​​​​​​
2
ответ дан 2 September 2019 в 22:52
поделиться
Другие вопросы по тегам:

Похожие вопросы: