1

请帮我一个...当页面加载时,如果$Id=pack单选按钮打包为选中和DIV PACK Display:Block;

如果$id =ind,则单选按钮 ind 为选中和DIV ind Display:Block;

<?PHP
    $id=$_GET['id'];
?>
<!DOCTYPE>
<html>
    <body>

        <label>Package</label>
        <input type="radio" name="package" value="pack"  
                <?php if($id=='pack') 
                    { 
                        echo"checked"; 
                        echo"<script type=\"text/javascript\">";
                        echo"document.getElementById(\"package\").style.display=\"block\"";
                        echo"</script>";
                     } ?> 
         >

        <label>Individual</label>
        <input type="radio" name="package" value="ind" 
                 <?php if($id=='ind') 
                     {
                       echo"checked"; 
                       echo"<script type=\"text/javascript\">";
                       echo"document.getElementById(\"individual\").style.display=\"block\"";
                       echo"</script>";
                      } ?> 
         >

        <div id="package" style=" display:none; height:100px; background-color:#a00; color:#fff;">
            Display this div if PACKAGE is checked
        </div>

        <div id="individual"  style=" display:none; height:100px; background-color:#a00; color:#fff;">
            Display this div if INDIVIDUAL is checked
        </div>
    </body>
</html>
4

3 回答 3

0

尝试这个:

<?PHP
    $id=$_GET['id'];
?>
<!DOCTYPE>
<html>
    <body>

        <label>Package</label>
        <input type="radio" name="package" value="pack"  >



        <label>Individual</label>
        <input type="radio" name="package" value="ind"   >



        <div id="package" style=" display:none; height:100px; background-color:#a00; color:#fff;">
            Display this div if PACKAGE is checked
        </div>

        <div id="individual"  style=" display:none; height:100px; background-color:#a00; color:#fff;">
            Display this div if INDIVIDUAL is checked
        </div>
                        <?php if($id=='pack') 
                    { 
                        echo"checked"; 
                        echo"<script type=\"text/javascript\">";
                        echo"document.getElementById(\"package\").style.display=\"block\"";
                        echo"</script>";
                     } ?> 
                                      <?php if($id=='ind') 
                     {
                       echo"checked"; 
                       echo"<script type=\"text/javascript\">";
                       echo"document.getElementById(\"individual\").style.display=\"block\"";
                       echo"</script>";
                      } ?> 
    </body>
</html>
于 2013-10-09T07:08:44.127 回答
0

You could do the whole "click radio show/hide that div" thing on the client side with a bit of CSS and jQuery.

$(document).ready(function () {
     $("input[type=radio]").click(function () {
        var showId = $(this).val() === 'pack' ? 'package' : 'individual';
        var hideId = (showId === 'package') ? 'individual' : 'package';
        $('#' + showId).show();
        $('#' + hideId).hide();
    });
});

Here is a DEMO

Instead of hide() and show() you can also use toggle() or toggleClass() in combination with a 'show' or 'hide' CSS class as Sudhir pointed out.

A solution like this keeps your code a bit cleaner imho :)

于 2013-10-09T07:42:26.487 回答
-1

你不需要 js,你可以这样做,创建 css 类

.hide {
  display: none;
}
.show {
  display: block
}

并在 php 文件中

$packChecked = ($id == "pack") ? "checked='checked'" : "";
$indChecked = ($id == "ind") ? "checked='checked'" : "";
$packClass = ($id == "pack") ? "show" : "hide";
$indClass = ($id == "ind") ? "show" : "hide";
then apply, like:
<input type="radio" name="package" value="pack" <?php echo $packChecked; ?> />
<div id="package" class="<?php echo $packClass; ?>">
...
</div>
//similarly for another radio button
于 2013-10-09T07:07:28.453 回答