jQuery prevent change for select

前端 未结 13 1374
無奈伤痛
無奈伤痛 2020-11-28 10:51

I want to prevent a select box from being changed if a certain condition applies. Doing this doesn\'t seem to work:

$(\'#my_select\').bind(\'change\', funct         


        
13条回答
  •  暗喜
    暗喜 (楼主)
    2020-11-28 11:20

    Another option to consider is disabling it when you do not want it to be able to be changed and enabling it:

    //When select should be disabled:
    {
        $('#my_select').attr('disabled', 'disabled');
    }
    
    //When select should not be disabled
    {
        $('#my_select').removeAttr('disabled');
    }
    

    Update since your comment (if I understand the functionality you want):

    $("#dropdown").change(function()
    {
        var answer = confirm("Are you sure you want to change your selection?")
        {
            if(answer)
            {
                //Update dropdown (Perform update logic)
            }
            else
            {
                //Allow Change (Do nothing - allow change)
            } 
        }            
    }); 
    

    Demo

提交回复
热议问题