将只读属性添加到所有表单元素 [英] Adding readonly attribute to all form elements

查看:90
本文介绍了将只读属性添加到所有表单元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用jQuery为所有表单元素添加只读属性,但似乎无法弄清楚如何执行此操作。



这是我' (尝试:

  $('#form1')。each(function(){$(this).attr('readonly ',true);}); 

我有一个使用label / input来显示表单元素的简单表单。
另外我还使用 tipsy (工具提示插件)以及<一个href =http://sonspring.com/journal/formalize-css =noreferrer>形式化(外观和感觉插件)

解决方案

试试这个:

$ $ $ '只读','只读');




  • 您可能希望包含更多元素# form1输入,#form1 textarea,#form1选择

  • 在jQuery中,您通常不需要遍历集合。 attr 可以用于与单个元素相同的集合。

  • 在您的情况下,#form1 只匹配< form> 元素,并且每个被触发 / em>,这个元素。要找到 all 元素(输入或不输入),您可以编写#form1 *


I'm using jQuery to add a readonly attribute to all form elements but can't seem to figure out how to do this.

Here is what I'm trying:

$('#form1').each( function() { $(this).attr('readonly', true); });

I have a simple form using label/input to display form elements. Also I'm using tipsy (Tool tip plug-in) as well as Formalize (Look and Feel Plug-in)

解决方案

Try this:

$('#form1 input').attr('readonly', 'readonly');

  • You may want to include more elements #form1 input, #form1 textarea, #form1 select
  • In jQuery, you usually don't need to iterate over the collection. attr would work for a collection same as for a single element.
  • In your case, #form1 matched just the <form> element, and each was triggered once, for that element. To find all elements (input or not), you can write #form1 *.

这篇关于将只读属性添加到所有表单元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆