我有两页HTML表单。第一页有一个提交表单,第二页有一个确认表单。第一个表单提供了许多控件的选择,而第二个页面再次显示来自提交表单的数据,并显示确认消息。在第二种形式中,所有字段必须是静态的。

从我所看到的,一些表单控件可以是只读的,所有控件都可以被禁用,区别在于你仍然可以选择一个只读字段。

而不是按字段做这个是否有任何方法标记整个表单为只读/禁用/静态,这样用户就不能改变任何控件?


当前回答

没有内置的方法,我知道这样做,所以你需要提出一个自定义解决方案,取决于你的表单有多复杂。你应该读读这篇文章:

将HTML表单转换为只读(更新:损坏的文章链接,存档链接)

EDIT: Based on your update, why are you so worried about having it read-only? You can do it via client-side but if not you will have to add the required tag to each control or convert the data and display it as raw text with no controls. If you are trying to make it read-only so that the next post will be unmodified then you have a problem because anyone can mess with the post to produce whatever they want so when you do in fact finally receive the data you better be checking it again to make sure it is valid.

其他回答

所有浏览器都支持的另一种简单方式是:

HTML:

<form class="disabled">
  <input type="text" name="name" />
  <input type="radio" name="gender" value="male">
  <input type="radio" name="gender" value="female">
  <input type="checkbox" name="vegetarian">
</form>

CSS:

.disabled {
  pointer-events: none;
  opacity: .4;
}

但是请注意,这种方法下的标签仍然有效,用户仍然可以操作带有焦点的元素。

你可以使用这个函数禁用表单:

function disableForm(formID){
  $('#' + formID).children(':input').attr('disabled', 'disabled');
}

点击这里查看工作演示

注意,它使用jQuery。

不是所有的表单元素都可以设置为只读,例如:

复选框 无线电盒子 文件上传 …更多. .

那么合理的解决方案是将所有表单元素的disabled属性设置为true,因为OP没有声明应该将特定的“锁定”表单发送到服务器(disabled属性不允许这样做)。

另一种解决方案,如下面的演示所示,是在表单元素的顶部放置一个层,这将阻止与表单元素内所有元素的任何交互,因为该层设置了更大的z-index值:

演示:

var form = document.forms[0], // form element to be "readonly" btn1 = document.querySelectorAll('button')[0], btn2 = document.querySelectorAll('button')[1] btn1.addEventListener('click', lockForm) btn2.addEventListener('click', lockFormByCSS) function lockForm(){ btn1.classList.toggle('on'); [].slice.call( form.elements ).forEach(function(item){ item.disabled = !item.disabled; }); } function lockFormByCSS(){ btn2.classList.toggle('on'); form.classList.toggle('lock'); } form{ position:relative; } form.lock::before{ content:''; position:absolute; z-index:999; top:0; right:0; bottom:0; left:0; } button.on{ color:red; } <button type='button'>Lock / Unlock Form</button> <button type='button'>Lock / Unlock Form (with CSS)</button> <br><br> <form> <fieldset> <legend>Some Form</legend> <input placeholder='text input'> <br><br> <input type='file'> <br><br> <textarea placeholder='textarea'></textarea> <br><br> <label><input type='checkbox'>Checkbox</label> <br><br> <label><input type='radio' name='r'>option 1</label> <label><input type='radio' name='r' checked>option 2</label> <label><input type='radio' name='r'>option 3</label> <br><br> <select> <option>options 1</option> <option>options 2</option> <option selected>options 3</option> </select> </fieldset> </form>

<form inert>

这不会改变表单的样式,但会阻止所有输入的可聚焦性和任何按钮的可点击性。

没有内置的方法,我知道这样做,所以你需要提出一个自定义解决方案,取决于你的表单有多复杂。你应该读读这篇文章:

将HTML表单转换为只读(更新:损坏的文章链接,存档链接)

EDIT: Based on your update, why are you so worried about having it read-only? You can do it via client-side but if not you will have to add the required tag to each control or convert the data and display it as raw text with no controls. If you are trying to make it read-only so that the next post will be unmodified then you have a problem because anyone can mess with the post to produce whatever they want so when you do in fact finally receive the data you better be checking it again to make sure it is valid.