Search code examples
jqueryserver-sidebackendhtml-selectjavascript

Disable drop down list by Jquery but maintain the value in server side


Good day all,

I tried to disable a drop down list by jQuery, and I would like to maintain the value in the drop down list in server side/ back-end although it has been disable.

This is my jQuery code:

if('${actionBean.list1.size()}' > 0)
    $('#dropdownA').prop("disabled", true);

However, after I disable the drop down, the value become null in my java file :

System.out.println("dropdown value is " + getdropdownA());

Any idea to maintain the value although I disable the drop down list after user selected on it?


Solution

  • Disabled elements do not get submitted with forms (hence the null value), so the easiest way to get around this would be to use a hidden element.

    Something like:

    if('${actionBean.list1.size()}' > 0){
      var dropDown = $('#dropdownA');
      $("myHiddenElement").val(dropDown.val());
      dropDown.prop("disabled", true);
    }
    

    Then you can check if dropDown has a value on the server and if not, default to myHiddenElement.