Readonly和Disabled的区别(小技巧)

 

1.js中为disabled,C#中为Enabled

2.在js中为readOnly

C#中为ReadOnly

而且如果你现在C#服务器中设置ReadOnly的话,客户端改变的值在服务器用(TextBox1.Text等因为viewstate的原因)也是读不到的,用Request.Form["TextBox1"];可以取到,用js设置readOnly在服务器可以读取到

 

Readonly和Disabled是用在表单中的两个属性,它们都能够做到使用户不能够更改表单域中的内容。但是它们之间有着微小的差别,总结如下:

Readonly只针对input(text / password)和textarea有效,而disabled对于所有的表单元素都有效,包括select, radio, checkbox, button等。但是表单元素在使用了disabled后,当我们将表单以POST或GET的方式提交的话,这个元素的值不会被传递出去,而readonly会将该值传递出去(这种情况出现在我们将某个表单中的textarea元素设置为disabled或readonly,但是submit button却是可以使用的)。

一般比较常用的情况是:

①在某个表单中为用户预填了某个唯一识别代码,不允许用户改动,但是在提交时需要传递该值,此时应该将它的属性设置为readonly

②经常遇到当用户正式提交了表单后需要等待管理员的信息验证,这就不允许用户再更改表单中的数据,而是只能够查看,由于disabled的作用元素范围大,所以此时应该使用disabled,但同时应该注意的是要将submit button也disabled掉,否则只要用户按了这个按钮,如果在数据库操作页面中没有做完整性检测的话,数据库中的值就会被清除。如果说在这种情况下用readonly来代替disabled的话,若表单中只有input(text / password)和textarea元素,那还是可以的,如果存在其他发元素,比如select,用户可以在重新改写值后按回车键进行提交(回车是默认的submit触发按键)

③我们常常在用户按了提交按钮后,利用javascript将提交按钮disabled掉,这样可以防止网络条件比较差的环境下,用户反复点提交按钮导致数据冗余地存入数据库。

disabled和readonly这两个属性有一些共同之处,比如都设为true,则form属性将不能被编辑,往往在写js代码的时候容易混合使用这两个属性,其实他们之间是有一定区别的。

如果一个输入项的disabled设为true,则该表单输入项不能获取焦点,用户的所有操作(鼠标点击和键盘输入等)对该输入项都无效,最重要的一点是当提交表单时,这个表单输入项将不会被提交。

而readonly只是针对文本输入框这类可以输入文本的输入项,如果设为true,用户只是不能编辑对应的文本,但是仍然可以聚焦焦点,并且在提交表单的时候,该输入项会作为form的一项提交。

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<title>Untitled Document</title>

</head>

<body>

<form id="form1" name="form1" method="get" action="">

<input name="q1" type="text" id="q1" value="readonly" readonly="true" />

<input name="q2" type="text" disabled="disabled" id="q2" value="disabled" />

<input type="submit" name="Submit" value="Submit" />

</form>

</body>

</html>

小技巧:

diabled:可用readonly代替,background-color:#cccccc;加上灰色背景色就可以

<input id="mytext" type="text" value="我是不能用的">
<input type="button" value="disabled" onClick="javascript: document.all.mytext.disabled='false'">
<input type="button" value="enable" onClick="javascript: document.all.mytext.removeAttribute('disabled')">

注意:document.all.mytext.disabled=true;表单控件不能用

document.all.mytext.disabled=false;表单控件能用

<input id="mytext" type="text" value="我是能用的">
<input type="button" value="disable" onClick="if (mytext.disabled==false){ mytext.disabled=true;mytext.value='我是不能用的';this.value='enable'} else { mytext.disabled=false;mytext.value='我是能用的';this.value='disable'}">

ASP.NET 2.0中TextBox服务器控件的ReadOnly和Disabled属性
以前的ASP.NET 1.x版本中,设置为ReadOnly的TextBox控件在客户端更改了值后,在服务器端仍然可以得到修改后的值,但在ASP.NET 2.0中,这种做法已经限制。这是为了提高应用程序安全性所考虑的。下面就是TextBox控件获得数据的内部方法,由此可以看出ReadOnly的限制:
protected virtual bool LoadPostData(string postDataKey, NameValueCollection postCollection)
{
 
base.ValidateEvent(postDataKey);
 
string text1 = this.Text;
 
string text2 = postCollection[postDataKey];
 
if (!this.ReadOnly && !text1.Equals(text2, StringComparison.Ordinal))
 {
  
this.Text = text2;
  
return true;
 }
 
return false;
}

这里限制的只是Text属性,而没有限制提交数据的名称/值的NameValueCollection,因此,通过Request["表单名称"]的方法仍然可以得到值的。下面的例子充分说明了这一点,并且提供了既使用ReadOnly,又可以通过Text属性获得值的方法:

<%@ Page Language="C#" EnableViewState="false" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">

  protected 
void Button1_Click(object sender, EventArgs e)
  {
    Response.Write(
"<li>TextBox1 = " + TextBox1.Text);
    Response.Write(
"<li>TextBox2 = " + TextBox2.Text);
    Response.Write(
"<li>TextBox3 = " + TextBox3.Text);
    Response.Write(
"<li>Request.Form[TextBox1] = " + Request.Form[TextBox1.UniqueID]);
    Response.Write(
"<li>Request.Form[TextBox2] = " + Request.Form[TextBox2.UniqueID]);
    Response.Write(
"<li>Request.Form[TextBox3] = " + Request.Form[TextBox3.UniqueID]);
  }

  protected 
void Page_Load(object sender, EventArgs e)
  {
    TextBox3.Attributes.Add(
"readonly""readonly");
  }
</script>

<script type="text/javascript">
//<![CDATA[
function SetNewValue()
{
  document.getElementById(
'<%=TextBox1.ClientID %>').value = "TextBox1 new Value";
  document.getElementById(
'<%=TextBox2.ClientID %>').value = "TextBox2 new Value";
  document.getElementById(
'<%=TextBox3.ClientID %>').value = "TextBox3 new Value";
}
//]]>
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
  
<title>ASP.NET 2.0中TextBox控件与ReadOnly和Enabled属性</title>
</head>
<body>
  
<form id="form1" runat="server">
    
<span>TextBox1 ReadOnly:</span>
    
<asp:TextBox ID="TextBox1" runat="server" ReadOnly="True" Text="TextBox1 Old Value"></asp:TextBox><br />
    
<span>TextBox2 Enabled:</span>
    
<asp:TextBox ID="TextBox2" runat="server" Enabled="False" Text="TextBox2 Old Value"></asp:TextBox><br />
    
<span>TextBox3 ReadOnly:</span>
    
<asp:TextBox ID="TextBox3" runat="server" Text="TextBox3 Old Value"></asp:TextBox><br />
    
<br />
    
<asp:Button ID="Button2" runat="server" Text="修改新值" OnClientClick="SetNewValue();return false;" />
    
<asp:Button ID="Button1" runat="server" Text="提交" OnClick="Button1_Click" />
  
</form>
</body>
</html>

对于disabled的TextBox,在服务器端不能得到修改的值,如果实在要用这个属性,那之后使用隐藏表单域的方法来实现了。

ReadOnly属性的TextBox在客户端会展现成这样的标记:

<input readonly = "readonly">

Enabled属性的TextBox在客户端会展现成这样的标记:

<input disabled="disabled">

按照W3C的规范:http://www.w3.org/TR/REC-html40/interact/forms.html#h-17.12

设置为disabled的input将会有下面的限制:

  1. 不能接收焦点
  2. 使用tab键时将被跳过
  3. 可能不是successful的  

设置为readonly的input将会有下面的限制:

  1. 可以接收焦点但不能被修改
  2. 可以使用tab键进行导航
  3. 可能是successful的  

只有successful的表单元素才是有效数据,也即是可以进行提交。disabled和readonly的文本输入框只能通过脚本进行修改value属性。

 

原文地址:https://www.cnblogs.com/cuihongyu3503319/p/1611769.html