如何自定义水平 jQuery-mobile 单选按钮

2023-10-21前端开发问题
2

本文介绍了如何自定义水平 jQuery-mobile 单选按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我们的客户需要为 jQuery-mobile 定制设计单选按钮样式.实际上它与水平模式下默认的 jQuery-mobile 单选按钮设计非常相似.例如 jQuery-mobile 将水平单选按钮定义为

Our client need a custom design radio button style for the jQuery-mobile. Actually it's very similar to default jQuery-mobile radio button design in horizontal mode. For example jQuery-mobile define the horizontal radio button as

<fieldset data-role="controlgroup" data-type="horizontal">
    <legend>I like to buy and wear trendy must-haves</legend>
    <input type="radio" name="radio-choice" id="radio-choice-1" value="choice-1" checked="checked"  />
    <label for="radio-choice-1">1</label>

    <input type="radio" name="radio-choice" id="radio-choice-2" value="choice-2"  />
    <label for="radio-choice-2">2</label>

    <input type="radio" name="radio-choice" id="radio-choice-3" value="choice-3"  />
    <label for="radio-choice-3">3</label>

    <input type="radio" name="radio-choice" id="radio-choice-4" value="choice-4"  />
    <label for="radio-choice-4">4</label>

    <input type="radio" name="radio-choice" id="radio-choice-5" value="choice-5"  />
    <label for="radio-choice-5">5</label>
</fieldset>

此外,我们的客户希望在 labell 下方显示默认单选按钮.例如下面的图片

Additionally our client wants to display the default radio button below the lablel. For example following image

我想知道 jQuery-mobile 是否允许我们显示默认单选按钮?如果可以,你能举个例子吗?

I would like to know does jQuery-mobile allow us to display the default radio buttons ? If so can you give an example ?

还是我们需要自定义这个?

Or should we need to customize this ?

推荐答案

解决方案

首先,它不能通过 jQM 配置来完成.这必须通过 jquery 手动创建(与任何其他 jQM 小部件一样,包括字段集).

Solution

First thing, it can't be done through the jQM configuration. This must be created manually through a jquery (like any other jQM widget, including fieldset).

我为您创建了一个工作示例:http://jsfiddle.net/Gajotres/779Kn/

I have created an working example for you: http://jsfiddle.net/Gajotres/779Kn/

您只需要再做一件事,我不想打扰自定义图像,所以我使用为我的另一个示例创建的图像:https://stackoverflow.com/a/13634738/1848600

Your only need to do one more thing, I didn't want to bother with a custom images so I am using images created for my other example: https://stackoverflow.com/a/13634738/1848600

这里需要一个javascript:

Here's an javascript needed:

$(document).on('pagebeforeshow', '#index', function(){     
    $('<div>').addClass('checkBox').appendTo('fieldset div div.ui-radio label');
    $('input[type="radio"]').each(function(){        
        ($(this).is(':checked')) ? $(this).next().find(".checkBox").addClass('checked') : $(this).next().find(".checkBox").addClass('not-checked');
    });
    
    $(document).on('click', '.ui-radio', function(){      
        $('input[type="radio"]').each(function(){  
            $(this).next().find(".checkBox").addClass('not-checked').removeClass('checked');
        }); 
        
        if($(this).find('input[type="radio"]').is(':checked')){
           $(this).find('label div.checkBox').removeClass('checked').addClass('not-checked'); 
           $(this).find('input[type="radio"]').attr('checked' , false);
        } else {
           $(this).find('label div.checkBox').removeClass('not-checked').addClass('checked');             
           $(this).find('input[type="radio"]').attr('checked' , true);
        }        
    });  
});

这是css:

.checkBox{
    width: 18px;
    height: 18px;
    background: #d9d9d9;
    border-radius: 3px;  
    margin: 0 auto;
    margin-bottom: 5px;
}

.not-checked, .checked {
    background-image: url("http://www.fajrunt.org/icons-18-white.png");
    background-repeat: no-repeat;
}

.not-checked {
    background-position: -18px 0;       
    background-color:#d9d9d9;
}

.checked {
    background-position: -720px 0;    
    background-color:#6294bc;
}

如果你能等几个小时,我会更新一张图片,我不能从我当前的位置更新.

If you can wait few hours I will update a picture, I can't do it from my current location.

如果您想了解有关如何自定义 jQuery Mobile 页面和小部件的更多信息,请查看此 文章.它附带了许多工作示例,包括为什么 !important 对于 jQuery Mobile 是必需的.

If you want to find more about how to customize jQuery Mobile page and widgets then take a look at this article. It comes with a lot of working examples, including why is !important necessary for jQuery Mobile.

这篇关于如何自定义水平 jQuery-mobile 单选按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

layui 实现实时刷新一个外部的div
主页面上显示了一个合计,在删除和增加的时候需要更改这个总套数的值: //html代码div class="layui-inline layui-show-xs-block" style="margin-left: 10px" id="sumDiv"spanSOP合计:/spanspan${totalNum}/spanspan套/span/div 于是在我们删除这个条数据后,...
2024-11-14 前端开发问题
156

layui 单选框、复选框、下拉菜单不显示问题如何解决?
1. 如果是ajax嵌套了 页面, 请确保 只有最外层的页面引入了layui.css 和 layui.js ,内层页面 切记不要再次引入 2. 具体代码如下 layui.use(['form', 'upload'], function(){ var form = layui.form; form.render(); // 加入这一句});...
2024-11-09 前端开发问题
313

layui要如何改变时间日历布局大小?
问题描述 我想改变layui时间日历布局大小,这个要怎么操作呢? 解决办法 可以用css样式对时间日历进行重新布局,具体代码如下: !DOCTYPE htmlhtmlheadmeta charset="UTF-8"title/titlelink rel="stylesheet" href="../../layui/css/layui.css" /style#test-...
2024-10-24 前端开发问题
271

layui中表单会自动刷新的问题
layui中表单会自动刷新的问题,因为用到layui的表单,遇到了刷新的问题所以记录一下: script layui.use(['jquery','form','layer'], function(){ var $ = layui.jquery, layer=layui.layer, form = layui.form; form.on('submit(tijiao)', function(data){ a...
2024-10-23 前端开发问题
262

jQuery怎么动态向页面添加代码?
append() 方法在被选元素的结尾(仍然在内部)插入指定内容。 语法: $(selector).append( content ) var creatPrintList = function(data){ var innerHtml = ""; for(var i =0;i data.length;i++){ innerHtml +="li class='contentLi'"; innerHtml +="a href...
2024-10-18 前端开发问题
125

“数组中的每个孩子都应该有一个唯一的 key prop"仅在第一次呈现页面时
quot;Each child in an array should have a unique key propquot; only on first time render of page(“数组中的每个孩子都应该有一个唯一的 key prop仅在第一次呈现页面时)...
2024-04-20 前端开发问题
5