12/9/11

Thiết lập thuộc tính ReadOnly cho ASP.NET Textboxes với JQuery

Trong bài này chúng ta sẽ thiết lập cho ASP.NET Textbox thuộc tính ReadOnly sử dụng JQuery


B1: Thêm thư viện JQuery
<script type="text/javascript" src="http://ajax.microsoft.com/ajax/jquery/jquery-1.5.1.min.js"></script>
B2: Viết sự kiện PageLoad cho Javascript
<script type="text/javascript">
$(function () {
$('input:text[value!=]').each(function () {
$(this).attr('readonly', true);
});
});
</script>
Hàm này sẽ đi kiểm tra trong mỗi Textbox nếu giá trị bằng rỗng thì thêm thuộc tính ReadOnly vào Textbox đó

Code
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Make TextBoxes ReadOnly at RunTime</title>

<script type="text/javascript"
src="http://ajax.microsoft.com/ajax/jquer...y-1.5.1.min.js">
</script>

<script type="text/javascript">
$(function () {
$('input:text[value!=]').each(function () {
$(this).attr('readonly', true);
});
});
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<h2>TextBoxes ReadOnly at RunTime</h2><br />
<asp:TextBox ID="tbox1" runat="server" Text="SomeText"/><br />
<asp:TextBox ID="tbox2" runat="server" Text=""/><br />
<asp:TextBox ID="tbox3" runat="server" Text="SomeText"/><br />
<br /><br />
Here only those TextBoxes with some value in it,
are made <br /> read-only and cannot be edited
</div>
</form>
</body>
</html>
Bookmark and Share

0 comments:

Post a Comment

Next previous home

Cộng đồng yêu thiết kế Việt Nam Thiet ke website, danang