使用单选按钮显示特定图像

就一次

我使用echo在两个不同的目录中显示和添加所有图像的类名(一个包含帽衫,另一个包含T恤衫)。我想要一个单选按钮,当单击该按钮时,仅显示该特定类别(例如,如果您单击T恤衫,则仅显示T恤衫。

                <input id="all"         type="radio" name="category">           <label for="all">All</label>                <br/>
                <input id="hoodies"     type="radio" name="category"/>          <label for="hoodies">Hoodies</label>        <br/>
                <input id="teeshirts"   type="radio" name="category">           <label for="teeshirts">Teeshirts</label>    <br/>

                <!-- Images -->
                <?php
                    // Teeshirts
                    $dir    = 'images/clothing/hoodies/small/';
                    $files = scandir($dir);
                    $images = array();
                    foreach($files as $file) {
                      if(fnmatch('*.png',$file)) {
                        $images[] = $file;
                      }
                      if(fnmatch('*.jpg',$file)) {
                        $images[] = $file;
                      }
                    }

                    foreach($images as $image) {
                      echo '<img src="images/clothing/hoodies/small/'.$image.'"width="15%" height="20%" hspace="2%" vspace="2%" class="hoodie" data-big="images/clothing/hoodies/large/'.$image.'" />';
                      // echo '<p>'.$image.'</p>';
                    }

                    // Hoodies
                    $dir    = 'images/clothing/teeshirts/small/';
                    $files = scandir($dir);
                    $images = array();
                    foreach($files as $file) {
                      if(fnmatch('*.png',$file)) {
                        $images[] = $file;
                      }
                      if(fnmatch('*.jpg',$file)) {
                        $images[] = $file;
                      }
                    }

                    foreach($images as $image) {
                      echo '<img src="images/clothing/teeshirts/small/'.$image.'" width="15%" height="20%" hspace="2%" vspace="2%" class="teeshirt" data-big="images/clothing/teeshirts/large/'.$image.'" />';
                    }                           
                ?>

这是我目前的CSS

#teeshirts:checked ~ .hoodie {
    display: none;
}

#hoodies:checked ~ .teeshirt {
    display: none;
}

现在,它的工作方式是说,如果您单击T恤衫,则不要显示连帽衫(仅显示T恤衫)。但是现在这根本不起作用(在我对每个图像进行硬编码之前,它曾经使用过,但是现在我在使用php时就不使用了)。我该如何工作?以及如何更好地编写它(所以它只显示一个类,而不显示另一个类)。

我也迷失了如何使所有按钮正常工作。

卡西夫·拉蒂夫(Kashif Latif)

这可以是使用原始数据的快速解决方案,您可以轻松地使用PHP

HTML代码

<div>
    <label><input type="radio" name="colorRadio" value="red"> red</label>
    <label><input type="radio" name="colorRadio" value="green"> green</label>
    <label><input type="radio" name="colorRadio" value="blue"> blue</label>
</div>
<div class="red box">You have selected <strong>red radio button</strong> so i am here</div>
<div class="green box">You have selected <strong>green radio button</strong> so i am here</div>
<div class="blue box">You have selected <strong>blue radio button</strong> so i am here</div>

CSS代码

<style type="text/css">
.box{
    padding: 20px;
    display: none;
    margin-top: 20px;
    border: 1px solid #000;
}
.red{ background: #ff0000; }
.green{ background: #00ff00; }
.blue{ background: #0000ff; }
</style>

也尝试此脚本

<script type="text/javascript" src="http://code.jquery.com/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$('input[type="radio"]').click(function(){
    if($(this).attr("value")=="red"){
        $(".box").not(".red").hide();
        $(".red").show();
    }
    if($(this).attr("value")=="green"){
        $(".box").not(".green").hide();
        $(".green").show();
    }
    if($(this).attr("value")=="blue"){
        $(".box").not(".blue").hide();
        $(".blue").show();
    }
});
});
</script>

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

使用FormHelper显示单选按钮

来自分类Dev

使用FormHelper显示单选按钮

来自分类Dev

图像单选按钮检查的显示问题

来自分类Dev

仅在选择特定的一组单选按钮中的至少一个时显示图像

来自分类Dev

对齐单选按钮中使用的内容(图像),以使按钮不会显示

来自分类Dev

对齐单选按钮中使用的内容(图像),以使按钮不会显示

来自分类Dev

如何使用ui:repeat显示单选按钮?

来自分类Dev

如何显示使用变量选择的单选按钮?

来自分类Dev

使用ipywidgets水平显示单选按钮

来自分类Dev

使用jQuery显示单选按钮的值

来自分类Dev

使用jQuery仅显示必需的单选按钮

来自分类Dev

jQuery使用单选按钮隐藏和显示

来自分类Dev

使用单选按钮显示/隐藏画布

来自分类Dev

使用相关的单选按钮 jquery 显示数据

来自分类Dev

如果选中组合单选按钮,如何显示不同的图像?

来自分类Dev

单击单选按钮时不会显示图像-损坏的Javascript

来自分类Dev

单击时突出显示图像,然后选中单选按钮

来自分类Dev

如何根据选择的单选按钮交换显示的图像?

来自分类Dev

根据选定的单选按钮在 HTML 画布上显示不同的图像

来自分类Dev

自定义单选按钮:为什么不显示图像?

来自分类Dev

带图像的单选按钮

来自分类Dev

图像作为单选按钮

来自分类Dev

根据单选按钮显示 HTML 代码的特定部分?

来自分类Dev

根据多个选中的单选按钮显示特定 div

来自分类Dev

单选按钮显示div

来自分类Dev

单选按钮不显示

来自分类Dev

单选按钮显示div

来自分类Dev

当使用CSS单击特定的单选按钮时,有没有一种显示文本的方法?

来自分类Dev

使用中心图像制作自定义单选按钮

Related 相关文章

热门标签

归档