1

Another topic on the much discussed issue of button responsiveness when web apps are used on mobile devices.

I am listening for the touchend event to trigger a radio button being pressed. This solves the issue of making the button more responsive, however creates another problem.

jQuery mobile applies classes such as ui-icon-radio-on, ui-radio-on, ui-btn-hvr-a, ui-btn-dwn-a when the event occurs that get left behind. This makes the button look like it is still being pressed even though the event is over. It ends up being a decent effort to juggle removing and adding all those classes to make everything look right.

My questions is - does anyone have an elegant way of adding and removing the needed classes and attributes.

or

Is there a better way of going about this that will not involve "recreating the wheel" in terms of manually dealing with the styling based on event triggers. Would google's fast button be a better solution? (not sure how to integrate). Is there a simpler way?

$(document).on('pageinit pageshow', 'div:jqmData(role="page"), div:jqmData(role="dialog")', function (event) {
if($(this).hasClass('AdminSurv') && event.type=='pageinit') {
    $(this).on( 'touchend', '.ui-radio', function(event) {
        event.preventDefault();

        /*uncheck all radios in control group to avoid multiple checks*/
        var _control_group = $(this).parent();
        _control_group.find("input:radio:checked").attr('checked',false);

        /*check the radio*/
        $(this).find('input').attr('checked', true);

        /*much juggling of classes/attributes going on here
          still looks like the buttons are being held down
          this is a very sloppy example of my initial attempt*/
        _control_group.find('label').removeClass('ui-radio-on');
        _control_group.find('label').removeAttr('data-icon');
        _control_group.find('label span span').removeClass('ui-icon-radio-on');

        $(this).find('label').removeClass('ui-radio-off');
        $(this).find('label').addClass('ui-radio-on');
        $(this).find('label').attr('data-icon','radio-on');

        $(this).find('label span span').removeClass('ui-icon-radio-off');
        $(this).find('label span span').addClass('ui-icon-radio-on');
    });
  }
});
4

1 回答 1

2

许多 jQuery Mobile 小部件接受refresh方法,其中它用于增强 DOM 中已存在或动态插入的元素的标记。

对于复选框和单选按钮,.checkboxradio('refresh')结合.prop()用于通过添加/删除类来增强/修改标记,用于动态选中和未选中的元素。

查看

$('.selector').prop('checked', true).checkboxradio('refresh');

取消选中

$('.selector').prop('checked', false).checkboxradio('refresh');

演示

参考:Checkboxradio 小部件

于 2013-05-24T20:20:20.410 回答