简体   繁体   English

在同一页面上使用jQuery Mobile Flip Switch切换具有隐藏的输入样式的图像

[英]Toggle an image with input style hidden using jQuery Mobile Flip Switch on the same page

Detail: I need to toggle an image ie when the flip switch is on, it shows the image, and hides it when the value if off. 详细信息:我需要切换图像,即当翻转开关打开时,它会显示图像,而在值关闭时将其隐藏。 The code is in a JS file. 该代码位于JS文件中。

$("#dashboard").append('<form><img src="../images/GREEN.png" style="display: none;" id="greenimage">Toggle Image<select id="flip" name="flip-select" data-role="flipswitch">'+
                    '<option>Off</option>'+
                    '<option>On</option>'+
                    '</select>'+
                '</img></form>');

$("#dashboard").trigger("create");          

                $('#flip').click(function() 
                        {

                            $("#yellowim").toggle('slow');
                        });

Putting checkpoints in the code shows that the CLICK function executes with the trigger event without even clicking the switch. 在代码中放置检查点表明,即使没有单击开关,CLICK函数也会在触发事件下执行。

You have a couple of problems. 你有几个问题。

First, IMG tag should close before the SELECT. 首先,IMG标签应在SELECT之前关闭。 Second, the ID you assigned to the image is 'greenimage', but you are trying to toggle 'yellowim'. 其次,您分配给图像的ID为“ greenimage”,但您尝试切换为“ yellowim”。 Third, as you are adding the select dynamically, you should use event delegation to bind the event. 第三,在动态添加选择时,应使用事件委托来绑定事件。 Finally, on a select you use the change event not the click event. 最后,在选择时使用更改事件而不是单击事件。

If you are using jQM 1.4.x, you can use enhanceWithin() instead of trigger("create"). 如果使用的是jQM 1.4.x,则可以使用EnhanceWithin()而不是trigger(“ create”)。

$("#dashboard").append('<form><img src="http://lorempixel.com/32/32/nature/1/" style="display: none;" id="greenimage" />Toggle Image<select id="flip" name="flip-select" data-role="flipswitch">' +
    '<option>Off</option>' +
    '<option>On</option>' +
    '</select>' +
    '</form>').enhanceWithin();

$("#dashboard").on("change", '#flip', function () {
    $("#greenimage").toggle('slow');
});

Here is a DEMO 这是一个演示

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

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