繁体   English   中英

在带有ui:repeat的表单中使用引导开关

[英]Using bootstrap-switch in a form with ui:repeat

我有一个显示产品列表的表格。

每个产品都可以选择或不选择。 用户必须选择他的产品,然后单击继续(提交)按钮。

为了表示选择,我使用了http://www.bootstrap-switch.org/

我的问题是,在加载页面时,默认情况下应该选择某些产品。

我为此提供的代码如下:

<ui:repeat value="#{bean.products}" var="product" varStatus="status">
    <li>
        <div>
            <div>
                <div>
                    <div style="width: 100%;">
                        <div>
                            <div><b>#{product.productName}</b></div>
                        </div>

                        <div>
                            <div class="make-switch switch-mini " data-on-label="Yes" data-off-label="No">
                                <input class="switch#{status.index}" type="checkbox"></input>
                            </div>
                            <input type="hidden" class="checked#{status.index}" value="#{product.initiallySelected}"></input>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </li>
</ui:repeat>

我有JavaScript,需要将最初选择的产品标记为“已选中”,但到目前为止,我尝试过的任何方法都没有效果。 这是我的JS:

$(document).ready(function() {
    flipSwitches(0);
}

function flipSwitches(index) {
    if ($(".checked"+index).length != 0) {
        var b = $(".checked"+index).val();
        alert(b);
        if (b == "true" || b == true) {
            alert("selected");
            // $(".switch"+index).attr('checked', true);
            // $(".switch"+index).bootstrapSwitch('toggleStatus');
            // $(".switch"+index).bootstrapSwitch('status');
            // $(".switch"+index).bootstrapSwitch('setState', true);
        }

        flipSwitches(index+1);
    }
}

我正在使用JSF 2.1(Apache MyFaces 2.1.5),JQuery 2.0.2,并在后端使用Java Bean。 如果有人可以帮助我,或者指出我做错了什么,那我将是超级!

添加id到输入即

<input id="myswitch" type="checkbox"/>

然后您可以通过

if(status==true){
    $('#myswitch').prop('checked',true);
}

那是我做的,对我有用。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM