开发者

Making Readonly all fields in a form

开发者 https://www.devze.com 2023-01-30 01:20 出处:网络
How can we make all fields in a form开发者_开发百科read only ?This should work: $(\':input\').attr(\'readonly\',\'readonly\');

How can we make all fields in a form开发者_开发百科 read only ?


This should work:

$(':input').attr('readonly','readonly');

Or if you have a specific form...

$('#myFormID :input').attr('readonly','readonly');

If you are using just plain JavaScript, you'll want to do this.

var f = document.forms['myFormNAME'];
for(var i=0,fLen=f.length;i<fLen;i++){
  f.elements[i].readOnly = true;//As @oldergod noted, the "O" must be upper case
}

One side note... although you can "set" the readonly flag on checkbox and hidden input fields... it won't actually make them readonly.


A solution without javascript, enclose all fields in fieldsets and add disabled tag to fieldsets.

0

精彩评论

暂无评论...
验证码 换一张
取 消