检测 jQuery Mobile 中的选择滑块更改事件

发布于 2024-11-27 14:27:28 字数 316 浏览 2 评论 0原文

检测 jQuery Mobile 中选择滑块更改的可靠方法是什么?我尝试绑定一个处理程序来更改选择控件本身的事件,但它在初始页面显示时触发,然后在单击时触发多次,有时甚至在悬停时触发(在桌面浏览器中)。

此行为的最小工作示例发布在此处: http://jsfiddle.net/NPC42/mTjtt/3 /

这可能是由于 jQuery Mobile 添加更多元素来将选择样式设置为翻转开关所致,但我找不到推荐的方法。

非常感谢任何帮助。

What is a reliable way to detect change of a select slider in jQuery Mobile? I try to bind a handler to change event of the select control itself, but it fires on initial page display, and then fires multiple times on clicks, and sometimes even on hover (in desktop browser).

The minimal working example of this behaviour is posted here: http://jsfiddle.net/NPC42/mTjtt/3/

This is probably caused by jQuery Mobile adding more elements to style the select as the flip-toggle, but I can't find the recommended way to do it.

Any help is much appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

池予 2024-12-04 14:27:28

可能不是最巧妙的解决方案,但它确实有效
http://jsfiddle.net/mTjtt/4/

May not be the slickest solution but it works
http://jsfiddle.net/mTjtt/4/

谁与争疯 2024-12-04 14:27:28

实例:

JS:

$('#my-slider').change(function(event) {
    event.stopPropagation();
    var myswitch = $(this);
    var show     = myswitch[0].selectedIndex == 1 ? true:false;

    if(show) {            
        $('#show-me').fadeIn('slow');
        $('#first-me').fadeOut();
    } else {            
        $('#first-me').fadeIn('slow');
        $('#show-me').fadeOut();
    }
});

HTML:

<div data-role="page" id="home" class="type-home"> 
    <div data-role="content"> 
        <div class="content-primary"> 
            <p>The flip toggle switch is displayed like this:</p> 
            <div data-role="fieldcontain"> 
                <label for="slider">Flip switch:</label> 
                <select name="slider" id="my-slider" data-role="slider"> 
                    <option value="off">Off</option> 
                    <option value="on">On</option> 
                </select> 
            </div> 
            <div id="first-me">
                <p>

Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
                </p>  
            </div>
            <div id="show-me" class="hidden">
                <p>
                    Bacon ipsum dolor sit amet bresaola velit laboris bacon eiusmod. Id ex short ribs, dolor dolore rump pork belly beef ad ullamco salami labore aute ut. Jowl et in do, fatback jerky salami reprehenderit irure laboris pork loin commodo qui eu. Chuck tri-tip cupidatat, turkey sunt in anim jerky pork belly exercitation bacon. Eu corned beef qui adipisicing, ground round veniam turkey chicken incididunt deserunt. Proident t-bone chuck, non excepteur biltong elit in anim minim swine short loin magna do. Sint enim nisi, minim nulla tongue ut incididunt ground round.
                </p>  
            </div>
        </div>
    </div>
</div>

更新:

我在这里提出了 jQM 的问题/错误:

  • < a href="https://github.com/jquery/jquery-mobile/issues/2188" rel="nofollow">https://github.com/jquery/jquery-mobile/issues/2188

Live Example:

JS:

$('#my-slider').change(function(event) {
    event.stopPropagation();
    var myswitch = $(this);
    var show     = myswitch[0].selectedIndex == 1 ? true:false;

    if(show) {            
        $('#show-me').fadeIn('slow');
        $('#first-me').fadeOut();
    } else {            
        $('#first-me').fadeIn('slow');
        $('#show-me').fadeOut();
    }
});

HTML:

<div data-role="page" id="home" class="type-home"> 
    <div data-role="content"> 
        <div class="content-primary"> 
            <p>The flip toggle switch is displayed like this:</p> 
            <div data-role="fieldcontain"> 
                <label for="slider">Flip switch:</label> 
                <select name="slider" id="my-slider" data-role="slider"> 
                    <option value="off">Off</option> 
                    <option value="on">On</option> 
                </select> 
            </div> 
            <div id="first-me">
                <p>

Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
                </p>  
            </div>
            <div id="show-me" class="hidden">
                <p>
                    Bacon ipsum dolor sit amet bresaola velit laboris bacon eiusmod. Id ex short ribs, dolor dolore rump pork belly beef ad ullamco salami labore aute ut. Jowl et in do, fatback jerky salami reprehenderit irure laboris pork loin commodo qui eu. Chuck tri-tip cupidatat, turkey sunt in anim jerky pork belly exercitation bacon. Eu corned beef qui adipisicing, ground round veniam turkey chicken incididunt deserunt. Proident t-bone chuck, non excepteur biltong elit in anim minim swine short loin magna do. Sint enim nisi, minim nulla tongue ut incididunt ground round.
                </p>  
            </div>
        </div>
    </div>
</div>

UPDATE:

I have raised an issue/bug with jQM here:

静谧幽蓝 2024-12-04 14:27:28

使用此代码,

$( ".mySliders" ).slider({
    create: function (event, ui) {
        $(this).bind('change', function () {
            ...
            ...
        });
    }
});

!不要将 type="range" 添加到您的输入标签中,而是将 type="text" 替换。

由于您是手动调用滑块函数。

Use this code,

$( ".mySliders" ).slider({
    create: function (event, ui) {
        $(this).bind('change', function () {
            ...
            ...
        });
    }
});

!Do not put type="range" to your input tags , put type="text" instead.

Since you are calling slider function manually.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文