toggle disabled attribute in jquery

前端 未结 3 900
臣服心动
臣服心动 2020-12-17 14:29

I\'ve a checkbox that enable or disable a select element

Actually I use this simple piece of code that works fine.

$(\"#filtri\").change(function(){
         


        
3条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-17 14:58

    You should use .prop for disabled:

    $("#menuContinenti").prop('disabled', function () {
       return ! $(this).prop('disabled');
    });
    

    UPDATE: didn't realize the current property value is an argument to the function; this version is even cleaner:

    $("#menuContinenti").prop('disabled', function (_, val) { return ! val; });
    

    UPDATE: ES2015

    $("#menuContinenti").prop("disabled", (_, val) => !val);
    

提交回复
热议问题