代码之家  ›  专栏  ›  技术社区  ›  eMRe

多重隐藏显示分区

  •  1
  • eMRe  · 技术社区  · 14 年前

    我已经试了一整天了。最后我设法让它工作。我知道这不是最好的办法。

    有人能告诉我一个更好的方法吗。我总共需要12个。它也不必是复选框。它可以只是一个文本。我的想法来自com/2006/12/14/using-jquery-to-show-hide-form-elements-based-on-a-checkbox-selection/

    我设法把它上传到 http://utilitybase.com/paste/wmq

    <!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>
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3/jquery.min.js" type="text/javascript"></script> 
    <script type="text/javascript">
        $(document).ready(function(){
    
            //Hide div w/id extra
           $("#extra").css("display","none");
    
            // Add onclick handler to checkbox w/id checkme
           $("#checkme").click(function(){
    
            // If checked
            if ($("#checkme").is(":checked"))
            {
                //show the hidden div
                $("#extra").show("fast");
            }
            else
            {      
                //otherwise, hide it 
                $("#extra").hide("fast");
            }
          });
    
        });
    </script>
    
    <script type="text/javascript">
        $(document).ready(function(){
    
            //Hide div w/id extra
           $("#extra1").css("display","none");
    
            // Add onclick handler to checkbox w/id checkme
           $("#checkme1").click(function(){
    
            // If checked
            if ($("#checkme1").is(":checked"))
            {
                //show the hidden div
                $("#extra1").show("fast");
            }
            else
            {      
                //otherwise, hide it 
                $("#extra1").hide("fast");
            }
          });
    
        });
    </script>
    
    <script type="text/javascript">
        $(document).ready(function(){
    
            //Hide div w/id extra
           $("#extra2").css("display","none");
    
            // Add onclick handler to checkbox w/id checkme
           $("#checkme2").click(function(){
    
            // If checked
            if ($("#checkme2").is(":checked"))
            {
                //show the hidden div
                $("#extra2").show("fast");
            }
            else
            {      
                //otherwise, hide it 
                $("#extra2").hide("fast");
            }
          });
    
        });
    </script>
    
    <script type="text/javascript">
        $(document).ready(function(){
    
            //Hide div w/id extra
           $("#extra3").css("display","none");
    
            // Add onclick handler to checkbox w/id checkme
           $("#checkme3").click(function(){
    
            // If checked
            if ($("#checkme3").is(":checked"))
            {
                //show the hidden div
                $("#extra3").show("fast");
            }
            else
            {      
                //otherwise, hide it 
                $("#extra3").hide("fast");
            }
          });
    
        });
    </script>
    
    <script type="text/javascript">
        $(document).ready(function(){
    
            //Hide div w/id extra
           $("#extra4").css("display","none");
    
            // Add onclick handler to checkbox w/id checkme
           $("#checkme4").click(function(){
    
            // If checked
            if ($("#checkme4").is(":checked"))
            {
                //show the hidden div
                $("#extra4").show("fast");
            }
            else
            {      
                //otherwise, hide it 
                $("#extra4").hide("fast");
            }
          });
    
        });
    </script>
    </head>
    
    <body>
    
    <div style="width: 800px;">
            <form>
    
                <input type="text" name="" maxlength="30" />
                <label for="checkbox"> Check to enter another email address:</label>
                <input id="checkme" type="checkbox" />
    
                <div id="extra">
                <input type="text" name="input" maxlength="30" />
                <label for="checkbox"> Check to enter another email address:</label>
                <input id="checkme1" type="checkbox" />
    
                <div id="extra1">
                <input type="text" name="" maxlength="30" />
                <label for="checkbox"> Check to enter another email address:</label>
                <input id="checkme2" type="checkbox" />
    
                <div id="extra2">
                <input type="text" name="" maxlength="30" />
                <label for="checkbox"> Check to enter another email address:</label>
                <input id="checkme3" type="checkbox" />
    
                <div id="extra3">
                <input type="text" name="" maxlength="30" />
                <label for="checkbox"> Check to enter another email address:</label>
                <input id="checkme4" type="checkbox" /> 
    
                <div id="extra4">
                <input type="text" name="" maxlength="30" />
                 </div>
               </div>
              </div>
             </div>
           </div>
      </form>
    </div>
    </body>
    </html>
    

    5 回复  |  直到 10 年前
        1
  •  2
  •   pex    14 年前

    如果我说对了,你可以和

    <head>
      <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.3/jquery.min.js" type="text/javascript"></script>
      <script type="text/javascript">
        $(document).ready(function(){
          var prototype = $('.prototype').clone();
    
          $('.prototype > input[type="checkbox"]').live('click', function() {
            if($(this).is(':checked')) {
              var clone = prototype.clone();
              clone.find('input[type="checkbox"]').attr('checked', false);
              $(this).parent('.prototype').append(clone);
            } else {
              $(this).parent('.prototype').find('.prototype:last').remove();
            }
          });
        });
      </script>
    </head>
    
    <body>
      <form>
        <div class="prototype">
          <input type="text" name="" maxlength="30" />
          <label for="checkbox"> Check to enter another email address:</label>
          <input id="checkme" type="checkbox" />
        </div>
      </form>
    </body>
    </html>
    

    :此添加在当前的输入/标签/复选框原型中是一个新的输入/标签/复选框原型,或者另一方面删除它的子级。您也可以将id添加到字段中。

        2
  •  0
  •   zod    14 年前

    很好。

    你试过了吗。每一次还是。活着

    http://api.jquery.com/each/

    http://api.jquery.com/live/

        3
  •  0
  •   Aaron Hathaway    14 年前

    嗯。在实用程序库上查看代码。也许试着用 .siblings() $(this).siblings('div').hide()/show();

        4
  •  0
  •   Adam    14 年前

    这通常对我有用

    $("div[id^='extra']").click(function(){
      $div = $(this);
      divID = $div.attr("id").substring(5);
    
    //Hide div w/id extra
           $div.css("display","none");
    
            // Add onclick handler to checkbox w/id checkme
           $("#checkme"+divID).click(function(){
    
            // If checked
            if ($("#checkme"+divID).is(":checked"))
            {
                //show the hidden div
                $div.show("fast");
            }
            else
            {      
                //otherwise, hide it 
                $div.hide("fast");
            }
    
    });
    

    这样就不必在多个timediv中编写同一个东西

        5
  •  0
  •   Ken Redler    14 年前

    // isolate the first div. You can surely find a better way,
    // using classes, containers, etc., but this works for now. 
    // Hide all the others.
    $('div:has(input):not(:first)').hide();
    
    // For each checkbox, open the next div
    $('input:checkbox').click( function(){
      if( $(this).is(':checked') ) {
        $(this).next('div').show('fast');
      } else {
        $(this).next('div').hide('fast');
      }
    });