asp.net如何实现textbox的readonly 属性 如题,在页面中有一个checkbox和一个textbox,我想当checkbox勾上的时候textbox可以输入,不勾的时候就readonly, 如何利用javascript实现?(checkbox 不是服务器控件 ) 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 <script type="text/javascript"> $(document).ready(function () { $("#ckbox").click(function () { if ($("#ckbox").attr("checked")) { $("#txt").attr("readonly", "readonly"); } else { $("#txt").removeAttr("readonly"); } }); }); </script> <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default5.aspx.cs" Inherits="_20120301_Default5" %><!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 runat="server"> <title>无标题页</title> <script src="../jquery-1.3.2-vsdoc.js" type="text/javascript"></script> <script type="text/javascript"> $(function(){ $("#Checkbox1").click(function(){ if($(this).attr("checked") == true) { $("#Text1").attr("readonly","readonly"); } else { $("#Text1").attr("readonly",""); } }); }); </script> </head><body> <form id="form1" runat="server"> <div> <input id="Checkbox1" type="checkbox" /> <input id="Text1" type="text" /> </div> </form></body></html> 这个,2楼给的反了<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default5.aspx.cs" Inherits="_20120301_Default5" %><!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 runat="server"> <title>无标题页</title> <script src="../jquery-1.3.2-vsdoc.js" type="text/javascript"></script> <script type="text/javascript"> $(function(){ $("#Checkbox1").click(function(){ if($(this).attr("checked") == true) { $("#Text1").attr("readonly",""); } else { $("#Text1").attr("readonly","readonly"); } }); }); </script> </head><body> <form id="form1" runat="server"> <div> <input id="Checkbox1" type="checkbox" /> <input id="Text1" type="text" readonly="readonly"/> </div> </form></body></html> 不用JQuery,用JS的方法<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default5.aspx.cs" Inherits="_20120301_Default5" %><!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 runat="server"> <title>无标题页</title> <script type="text/javascript"> function testFunc() { if(document.getElementById("Checkbox1").checked == true) { document.getElementById("Text1").readOnly = false; } else { document.getElementById("Text1").readOnly = true; } } </script></head><body> <form id="form1" runat="server"> <div> <input id="Checkbox1" type="checkbox" onclick="testFunc();" /> <input id="Text1" type="text" readonly="readonly" /> </div> </form></body> 因为我的textbox的值需要后台处理,所以我的textBox是服务器控件,以上方法实现不了啊...... 不都是一样么, document.getElementById("Control ID").readOnly 在byID,的时候用的是控件ID,js是可以获取到的啊.如果不用js,就用后台事件嘛!!!!checkbox的onchanged事件咯 服务器控件也可以用ID取得的$("#<%=textBox.ClientID%>").attr("readonly","readonly");要注意的是,要放在aspx或者ascx中才可以。 这是jquery的用法吧?我只想用js实现,没有引入jquery的库 $("checkbox").click(function(){if($(this).attr("checked")){$("textbox").attr("readonly","readonly");}else{$("textbox").removeAttr("readonly");}}); 1、2、3 楼的方法都是可以的 只需要把 document.getElementById("Text1") 改成 document.getElementById("<%= Text1.ClientID%>")就可以了checkbox 取id也是一样<asp:TextBox runat="server" id="Text1" /> HTML页面<asp:CheckBox runat="server" ID="ckbox" onclick="cbClick()"/> <asp:TextBox runat="server" ID="txt"></asp:TextBox>function cbClick() { if (document.getElementById("<%=ckbox.ClientID%>").checked) { document.getElementById("<%=txt.ClientID%>").readOnly = true; } else { document.getElementById("<%=txt.ClientID%>").readOnly = false; } } 服务器近件有什么不同吗?Jquery获取服务器控件 $("[id$=控件的ID]")JS获取document.getElementById("<%=控件的ID.ClientID%>") Repeater 控件出来的结果居然不是表格 网站页面中插入声音功能,如何实现? 整理各类IT编程视频教程下载地址(免费分享) 6rooms这种tag是怎么实现的? 请教高手如何写才能让EXCEL文件显示到页面上来呢? .net中 Database instance 用法 这是什么意思 简单的一段代码运行起来没错但为什么会提示"网页上有错误",帮忙看看 困域呀 关于dropdownlist控键的默认值 请给出asp.net使用SQL数据库的完整例子 需求书 asp.net 电子书素材
$(document).ready(function () { $("#ckbox").click(function () {
if ($("#ckbox").attr("checked")) {
$("#txt").attr("readonly", "readonly");
}
else {
$("#txt").removeAttr("readonly");
}
});
});
</script>
<head runat="server">
<title>无标题页</title> <script src="../jquery-1.3.2-vsdoc.js" type="text/javascript"></script>
<script type="text/javascript">
$(function(){
$("#Checkbox1").click(function(){
if($(this).attr("checked") == true)
{
$("#Text1").attr("readonly","readonly");
}
else
{
$("#Text1").attr("readonly","");
}
});
});
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<input id="Checkbox1" type="checkbox" />
<input id="Text1" type="text" />
</div>
</form>
</body>
</html>
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default5.aspx.cs" Inherits="_20120301_Default5" %><!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 runat="server">
<title>无标题页</title> <script src="../jquery-1.3.2-vsdoc.js" type="text/javascript"></script>
<script type="text/javascript">
$(function(){
$("#Checkbox1").click(function(){
if($(this).attr("checked") == true)
{
$("#Text1").attr("readonly","");
}
else
{
$("#Text1").attr("readonly","readonly");
}
});
});
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<input id="Checkbox1" type="checkbox" />
<input id="Text1" type="text" readonly="readonly"/>
</div>
</form>
</body>
</html>
不用JQuery,用JS的方法
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default5.aspx.cs" Inherits="_20120301_Default5" %><!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 runat="server">
<title>无标题页</title> <script type="text/javascript">
function testFunc()
{
if(document.getElementById("Checkbox1").checked == true)
{
document.getElementById("Text1").readOnly = false;
}
else
{
document.getElementById("Text1").readOnly = true;
}
}
</script></head>
<body>
<form id="form1" runat="server">
<div>
<input id="Checkbox1" type="checkbox" onclick="testFunc();" />
<input id="Text1" type="text" readonly="readonly" />
</div>
</form>
</body>
如果不用js,就用后台事件嘛!!!!checkbox的onchanged事件咯
服务器控件也可以用ID取得的$("#<%=textBox.ClientID%>").attr("readonly","readonly");要注意的是,要放在aspx或者ascx中才可以。
if($(this).attr("checked"))
{
$("textbox").attr("readonly","readonly");
}
else
{
$("textbox").removeAttr("readonly");
}
});
document.getElementById("<%= Text1.ClientID%>")就可以了checkbox 取id也是一样<asp:TextBox runat="server" id="Text1" />
HTML页面
<asp:CheckBox runat="server" ID="ckbox" onclick="cbClick()"/> <asp:TextBox runat="server" ID="txt"></asp:TextBox>function cbClick() {
if (document.getElementById("<%=ckbox.ClientID%>").checked) {
document.getElementById("<%=txt.ClientID%>").readOnly = true;
}
else {
document.getElementById("<%=txt.ClientID%>").readOnly = false;
}
}
服务器近件有什么不同吗?Jquery获取服务器控件 $("[id$=控件的ID]")JS获取document.getElementById("<%=控件的ID.ClientID%>")