How can I make an entire HTML form “readonly”?

2019-01-13 02:55发布

Edit again:

Surely this is standard stuff? I can't be reinventing the wheel??!! User fills in a form and you show it to him (using PHP, but that should not matter). You show it to him as confirmation, so he should not be able to attempt to change it again ...


See related question, How to display a form list box as read-only or disabled with a selected index? The gist of it is that I want to perform what must be a very common task ...

There are two forms - a submission form in HTML and a processing & acknowledgement form in PHP.

The first form offers a choice in many controls, the second verifies the input and, if valid, displays the input form again with a confirmation message. On this second form all fields must be static.

From what I can see, some form controls can be readonly and all can be disabled, the difference being that you can still tab to a readonly field.

Rather than doing this field by field is there anyway to mark the whole form as readonly/disabled/static such that the user can't alter any of the controls?


Edit: thanks for all the JS solutions (which I have +1) but I am restricted to a server-side solution. Sorry, I ought to have said this originally.


[Update] This is still getting answers seven years later ;-) What I did, in the end, was to generate the form from PHP and, for each field write a readonly or disabled attribute (as appropriate to the type of control), set to true or false according to to a global variable.

标签: html webforms
11条回答
手持菜刀,她持情操
2楼-- · 2019-01-13 03:27

Easiest way

$('#yourform *').prop('readonly', true);
查看更多
我想做一个坏孩纸
3楼-- · 2019-01-13 03:31

Wrap the input fields and other stuff into a <fieldset> and give it the disabled="disabled" attribute.

Example (http://jsfiddle.net/7qGHN/):

<form>
    <fieldset disabled="disabled">
        <input type="text" name="something" placeholder="enter some text" />
        <select>
            <option value="0" disabled="disabled" selected="selected">select somethihng</option>
            <option value="1">woot</option>
            <option value="2">is</option>
            <option value="3">this</option>
        </select>
    </fieldset>
</form>

查看更多
啃猪蹄的小仙女
4楼-- · 2019-01-13 03:32

On the confirmation page, don't put the content in editable controls, just write them to the page.

查看更多
甜甜的少女心
5楼-- · 2019-01-13 03:32

There's no fully compliant, official HTML way to do it, but a little javascript can go a long way. Another problem you'll run into is that disabled fields don't show up in the POST data

查看更多
可以哭但决不认输i
6楼-- · 2019-01-13 03:33

I'd rather use jQuery:

$('#'+formID).find(':input').attr('disabled', 'disabled');

find() would go much deeper till nth nested child than children(), which looks for immediate children only.

查看更多
登录 后发表回答