ChatGPT解决这个技术问题 Extra ChatGPT

HTML form readonly SELECT tag/input

According to HTML specs, the select tag in HTML doesn't have a readonly attribute, only a disabled attribute. So if you want to keep the user from changing the dropdown, you have to use disabled.

The only problem is that disabled HTML form inputs don't get included in the POST / GET data.

What's the best way to emulate the readonly attribute for a select tag, and still get the POST data?

Don't rely on that for the server side. Anybody can create their own HTML page and make it RW.
But it's not a PHP-specific question.
I would suggest not using a select element at all in this case. Is there any reason you can't just display the value as plain text?
@ppumkin your comment makes no sense. I'm not saying there's never a good use case for select or hidden form fields. The OP was having trouble displaying some text on the page, and I was simply wondering what the purpose was of using a select element in this case.
I must be reading the wrong question. He says he wants to disable the select so the user doesn't change it. Maybe he needs to render the page with selects and use jquery to prevent changes. But when he submits it back there is no data for it. I was doing the same. I need to display selects that is filtered by other selects and the last drop down saves to DB via ajax so all the previous must be locked down. When I re render the page, yea, OK- I could display labels instead of selects. But that is not the problem :)

b
bezmax

You should keep the select element disabled but also add another hidden input with the same name and value.

If you reenable your SELECT, you should copy its value to the hidden input in an onchange event and disable (or remove) the hidden input.

Here is a demo:

$('#mainform').submit(function() { $('#formdata_container').show(); $('#formdata').html($(this).serialize()); return false; }); $('#enableselect').click(function() { $('#mainform input[name=animal]') .attr("disabled", true); $('#animal-select') .attr('disabled', false) .attr('name', 'animal'); $('#enableselect').hide(); return false; }); #formdata_container { padding: 10px; }


If you reenable the select you must also then disable or remove the hidden input (after copying it's value as described) of course. Otherwise you'll get the value submitted double
@max Ah!. Ok, that works too. I had assumed since yo said that the hidden input should have the "same name" that the select had a name.
What if I'm using a multiple select?
Having two elements with the same names will only post back the last enabled input/selected, not double. Also, only the selected value gets posted back not the entire list. So, your hidden sits before your select and holds the selected value. If the select gets disabled for "readonly", the post back will only contain the value of the hidden input. If the select is enabled, the visible selected option will "over write/replace" the hidden value, and that is the value that will get posted back.
While this is the obvious solution, it sucks as a solution, since you have to add another input field.
G
Gleb Kemarsky

We could also disable all except the selected option.

This way the dropdown still works (and submits its value) but the user can not select another value.

Demo


Nice one for dynamic option's
Perfect for a single-value SELECT tag! But won't work for a

javascript ones:

selectElement.addEventListener("focus", selectElement.blur, true); selectElement.attachEvent("focus", selectElement.blur); //thanks, IE

to remove:

selectElement.removeEventListener("focus", selectElement.blur, true); selectElement.detachEvent("focus", selectElement.blur); //thanks, IE

edit: added remove methods


@ButtleButkus do the javascript ones work? i guess it can be a browser related issue.have you tried to add a tabindex to the element
HTML solution does not work on today's Chrome. :-(
M
Mario S

In IE I was able to defeat the onfocus=>onblur approach by double-clicking. But remembering the value and then restoring it in the onchange event seems to handle that issue.

<select onfocus="this.oldvalue=this.value;this.blur();" onchange="this.value=this.oldvalue;">
....
</select>

You can do similar without expando properties by using a javascript variable.


W
Wendell Carvalho

Simply, remove the disabled attribute before submit the form.

    $('form').submit(function () {
        $("#Id_Unidade").attr("disabled", false);
    });

A
Ataur Rahman Munna
<select id="case_reason" name="case_reason" disabled="disabled">

disabled="disabled" ->will get your value from database dan show it in the form. readonly="readonly" ->you can change your value in selectbox, but your value couldn't save in your database.


wrong, it's saved. Looks like the 'readonly' property isn't processed by all browsers and thus unreliable.