How to ensure a <select> form field is submitted when it is disabled?

I have a select form field that I want to mark as "readonly", as in the user cannot modify the value, but the value is still submitted with the form. Using the disabled attribute prevents the user from changing the value, but does not submit the value with the form.

The readonly attribute is only available for input and textarea fields, but that's basically what I want. Is there any way to get that working?

Two possibilities I'm considering include:

  • Instead of disabling the select , disable all of the option s and use CSS to gray out the select so it looks like its disabled.
  • Add a click event handler to the submit button so that it enables all of the disabled dropdown menus before submitting the form.

  • <select disabled="disabled">
        ....
    </select>
    <input type="hidden" name="select_name" value="selected value" />
    

    Where select_name is the name that you would normally give the <select> .

    Another option.

    <select name="myselect" disabled="disabled">
        <option value="myselectedvalue" selected="selected">My Value</option>
        ....
    </select>
    <input type="hidden" name="myselect" value="myselectedvalue" />
    

    Now with this one, I have noticed that depending on what webserver you are using, you may have to put the hidden input either before, or after the <select> .

    If my memory serves me correctly, with IIS, you put it before, with Apache you put it after. As always, testing is key.


    Disable the fields and then enable them before the form is submitted:

    jQuery code:

    jQuery(function ($) {        
      $('form').bind('submit', function () {
        $(this).find(':input').prop('disabled', false);
      });
    });
    

    I`ve been looking for a solution for this, and since i didnt find a solution in this thread i did my own.

    // With jQuery
    $('#selectbox').focus(function(e) {
        $(this).blur();
    });
    

    Simple, you just blur the field when you focus on it, something like disabling it, but you actually send its data.

    链接地址: http://www.djcxy.com/p/26450.html

    上一篇: 删除Chrome自动填充的输入背景颜色?

    下一篇: 如何确保<select>表单字段在禁用时提交?