當選中RadioButton時, 將TextBox控制項置為不可用

來源:互聯網
上載者:User
任務要求:在ASP.NET中,應用JavaScript實現:一組RadioButton,分別對應一個TextBox,當RadioButton被選中時,其後的TextBox置為不可用。

//testControlPage.aspx.cs

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="testControlPage.aspx.cs" Inherits="testControlPage" %>

<!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 language="javascript" type="text/javascript">
function RBchecked()
{
  var a = document.getElementsByName("a");
  for (var i=0; i<a.length; i++)
  {  
    if(a.checked)
      {
        var tb = document.getElementsByName("tb"+i);
        tb[0].readOnly=true;
      }
      else
      {
        var tbf = document.getElementsByName("tb"+i);
        if (tbf[0].readOnly==true)
          tbf[0].readOnly=false;
      }
  }
}
</script>
</head>
<body >
  <form id="form1" runat="server" >
  <div>
    <asp:RadioButton id=rb0 GroupName="a" runat="server"   />
    <asp:TextBox id=tb0 name="tb" runat="server"   />
   
    <asp:RadioButton id=rb1 GroupName="a" runat="server"   />
    <asp:TextBox id=tb1 name="tb" runat="server"   />
   
    <asp:RadioButton id=rb2 GroupName="a" runat="server"   />
    <asp:TextBox id=tb2 name="tb" runat="server"   />
   
    <input type=checkbox id=cb4 runat=server onclick="this.checked?test.disabled=true:test.disabled=false" />
        <input type=button id="test" value="測試" />
  </div>
  </form>
</body>
</html>

//testControlPage.cs
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class testControlPage : System.Web.UI.Page
{
  protected void Page_Load(object sender, EventArgs e)
  {
    rb0.Attributes.Add("onclick", "JavaScript:RBchecked();");
    rb1.Attributes.Add("onclick", "JavaScript:RBchecked();");
    rb2.Attributes.Add("onclick", "JavaScript:RBchecked();");
  }
}

qltouming(緣木漁人)
2007-04-11

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.