Use image for radio button selection in primefaces selectOneRadio

假如想象 提交于 2019-12-24 06:38:13

问题


I am trying to build UI using primefaces selectOneRadio. Currently I need to be able to display 5 images which are actually radio buttons and the image gets a blue border when selected. The actual radio button is hidden.

I tried primefaces selectOneRadio with custom layout but clicking on image does not select the radio button and the button is also visible.

Here is my code. I use foundation for css.

<ul class="small-block-grid-5">
            <p:selectOneRadio value="scooter" id="vehicleType" layout="custom">
                <f:selectItem itemValue="scooter" />
                <f:selectItem itemValue="bike"/>
                <f:selectItem itemValue="car"/>
                <f:selectItem itemValue="plane" />
                <f:selectItem itemValue="unknown" />
                <f:ajax event="click" render="addCost"/>
            </p:selectOneRadio>

            <li>
                <p:radioButton id="scooter" for="vehicleType" itemIndex="0"></p:radioButton>
                <h:graphicImage for="scooter" value="/images/scooter.GIF" width="50" height="50"/>
            </li>
            <li>
                <p:radioButton id="bike" for="vehicleType" itemIndex="1"></p:radioButton>
                <h:graphicImage for="bike" value="/images/bike.GIF" width="50" height="50"/>
            </li>
            <li>
                <p:radioButton id="car" for="vehicleType" itemIndex="2"></p:radioButton>
                <h:graphicImage for="car" value="/images/car.GIF" width="50" height="50"/>
                <h:panelGroup id="addCost">
                <h:outputText styleClass="breadcrumbs" value="Add 10$" />
                </h:panelGroup>
            </li>
            <li>
                <p:radioButton id="plane" for="vehicleType" itemIndex="3"></p:radioButton>
                <h:graphicImage for="plane" value="/images/plane.GIF" width="50" height="50"/>
            </li>
            <li>
                <p:radioButton id="unknown" for="vehicleType" itemIndex="4"></p:radioButton>
                <h:graphicImage for="unknown" value="/images/unkown.GIF" width="50" height="50"/>
            </li>
        </ul>

回答1:


I solved this using some css and foundation.

             <ul class="small-block-grid-5">
                <p:selectOneRadio value="scooter" id="vehicleType" layout="custom">
                    <f:selectItem itemValue="scooter" />
                    <f:selectItem itemValue="bike"/>
                    <f:selectItem itemValue="car"/>
                    <f:selectItem itemValue="plane" />
                    <f:selectItem itemValue="unknown" />
                    <f:ajax event="click" render="addCost"/>
                </p:selectOneRadio>

                <li>
                <div class="row">
                    <div class="small-1 columns invisibleDiv ">
                    <p:radioButton id="scooter" for="vehicleType" itemIndex="0"></p:radioButton>
                    </div>
                    <div class="small-11 columns imgSec">
                    <h:graphicImage for="scooter" value="/images/scooter.GIF" width="50" height="50"/>
                    </div>
                </div>
                </li>
            </ul>

The css

.invisibleDiv {
    visibility: hidden;
}
.borderDiv {
    border: 1px solid blue;
}

The js

$(".imgSec").click(function(){
        var radioInput= $(this).prev(".invisibleDiv ");
        $(".imgSec").removeClass("borderDiv");
        $(this).addClass("borderDiv");
    });


来源:https://stackoverflow.com/questions/38725709/use-image-for-radio-button-selection-in-primefaces-selectoneradio

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!