请教个问题,js如何实现当复选框选中状态时后面的文本框可编辑,当没选中时文本框文本框的值为空

解决方案 »

  1.   


    <!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>无标题文档</title>
    <script type="text/javascript" src="jquery-1.2.6.min.js"></script>
    </head>
    <body>
    <input type="checkbox" /><input type="text" /><br/>
    <input type="checkbox" /><input type="text" /><br/>
    <input type="checkbox" /><input type="text" /><br/>
    <input type="checkbox" /><input type="text" /><br/>
    <script type="text/javascript">
    window.onload = function()
    {
    var ipts = document.getElementsByTagName("input");
    for(var i=0;i<ipts.length;i++)
    {
    if(ipts[i].type=="text")
    {
    ipts[i].disabled = true;
    }
    else
    {
    ipts[i].onclick=function()
    {
    if(this.checked)
    this.nextSibling.disabled = false;
    else
    this.nextSibling.disabled = true;
    }
    }
    }
    }
    </script>
    </body>
    </html>
      

  2.   

    初学啊,问个幼稚的问题啊,this.nextSibling.disabled = false;
    中,nextSibling指的是下一个输入框???
      

  3.   

    nextSibling 属性可返回某个元素之后紧跟的元素(处于同一树层级中)。
    如果无此节点,则属性返回 null。http://www.w3school.com.cn/xmldom/prop_node_nextsibling.asp
      

  4.   


    <!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>无标题文档</title>
        <script type="text/javascript" src="jquery-1.2.6.min.js"></script>
    </head>
    <body>
    <input type="checkbox" /><input type="text" /><br/>
    <input type="checkbox" /><input type="text" /><br/>
    <input type="checkbox" /><input type="text" /><br/>
    <input type="checkbox" /><input type="text" /><br/>
    <script type="text/javascript">
    window.onload = function()
    {
        var ipts = document.getElementsByTagName("input");
        for(var i=0;i<ipts.length;i++)
        {
            if(ipts[i].type=="text")
            {
                ipts[i].disabled = true;
            }
            else
            {
                ipts[i].onclick=function()
                {
                    if(this.checked)
                        this.nextSibling.disabled = false;
                    else
                        this.nextSibling.disabled = true;
                }
            }
        }
    }
    </script>
    </body>
    </html>
      

  5.   

    好东西,学习,收藏博客了:http://www.itgoto.com.cn/?action=show&id=191
      

  6.   

    <input type="checkbox" onclick="this.nextSibling.disabled=!this.nextSibling.disabled" /><input disabled/>
      

  7.   


    <html xmlns="http://www.w3.org/1999/xhtml" >
    <head runat="server">
        <title>无标题页</title>
        <script type="text/javascript">
        function setText(obj)
        {
            if(obj.checked==true)
            {
                document.getElementById("txtName").disabled=false;
            }
            else
            {
                document.getElementById("txtName").disabled=true;
                document.getElementById("txtName").value="";
            }
        }
        
        </script>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
            <input id="Checkbox1" type="checkbox"  onclick="setText(this)" />  
            <input id="txtName" type="text" />
        </div>
        </form>
    </body>
    </html>
      

  8.   

      12楼的  一开始onclick没触发,text是可编辑的,有点小问题,呵呵
      

  9.   

    一开始 disabled="false"  就好了
      

  10.   

    12楼的也不对,要是考虑刷新加个onload函数即可了。
      

  11.   

    实用的东西,学习了,虽然Jquery方便,但还是知道基本原理之后再用的好。