ipad disable select options w/o jquery

2019-07-11 18:58发布

问题:

There is a way,plugin or code snippet that i could use to make

<select><option disable="disable">....</option></select>

options in safari on ipad to be disabled ?

latter edit: here is the code:

     function getSelectedValues(ids) {

    var selectedValues = []
    , $selected = $('.selector').children('option:selected');       

    $selected.each(function(){  
        if(this.value != 0){
            if(ids == true){
                selectedValues.push(this.value+'-'+$(this).parent().attr('id'));
            } else {
                selectedValues.push(this.value);
            }
        }           
    });     
    return selectedValues;  
}


function clearDisabled() {
    $('.selector').children(':disabled').attr('disabled', false);
}

function disableSelected(selectedValues,id) {

    sv = selectedValues || [];

    if(id === true){
        var selectedIds  = [];
        var selectedVals = [];

        $.each(selectedValues, function(key, value) { 
            values = value.split('-');

            selectedVals.push(values[0]);
            selectedIds.push(values[1]);

        });

        for (var i=0;i<selectedVals.length;i++) {
            $('.selector').each(function(){
                if($(this).attr('id') == selectedIds[i]){
                    $('option[value=' + selectedVals[i] + ']',this).not(':selected').attr('disabled', true);
                }
            });
        }
    }
}

$(document).ready(function(){
    $('.selector').change(function(){
        selectedValues = getSelectedValues(true);
        clearDisabled();
        disableSelected(selectedValues, true);
    });

    var selectedValues = getSelectedValues(true);
    disableSelected(selectedValues, true);
});

I did some digging and i realize that this is a limitation of safari mobile...

回答1:

$("select option[disable='disable']").attr("disabled","disabled");

or if you are using jquery 1.6 or higher

  $("select option[disable='disable']").prop("disabled",true);


回答2:

It is more complicated to get a cross browser implementation, safari for iPad and iPhone, ie6 and I presume some other browser just ignore the disabled options.

You need to remove them. So, keep a copy of it and rebuild the options whenever you want.

This is an example where you can enable/disable options based on their value

var $select = $("select#myselect");
$select.data("copy", $select.find("option"))

function disableOption($option) {
  var optionVal = $option.val();
  $select.empty()
  $select.data("copy").each(function(){
    var $currentOption = $(this);
    if (optionVal !== $currentOption.val() && $currentOption.attr("disabled")!=="disabled") $select.append($currentOption);
    else $currentOption.attr("disabled", "disabled");
  });
}


function enableOption($option) {
  var optionVal = $option.val();
  $select.empty()
  $select.data("copy").each(function(){
    var $currentOption = $(this);
    if (optionVal === $currentOption.val()) $currentOption.removeAttr("disabled")
    if ($currentOption.attr("disabled")!=="disabled") $select.append($currentOption);
  });
}