简体   繁体   English

如何从代码更改 jquery 移动翻转开关状态

[英]How to change the jquery mobile flip switch state from code

I have some jQuery Mobile flip toggle switches on my Android/iPad application, and I need to change their states (on/off) dynamically, using JavaScript.我的 Android/iPad 应用程序上有一些 jQuery Mobile 翻转切换开关,我需要使用 JavaScript 动态更改它们的状态(开/关)。 I was looking for a solution here, ( Change value of flip toggle dynamically with jQuery Mobile ) and I tried several ways ( .val('on') , .slider('enable') ...) but it seems the control is not working at all.我在这里寻找解决方案,( 使用 jQuery Mobile 动态更改翻转切换的值)并且我尝试了几种方法( .val('on') , .slider('enable') ...)但似乎控件是根本不工作。

Is there a solution for this issue?这个问题有解决方案吗? How can I do to change the flip switch state from code?如何从代码中更改翻转开关状态?

I've examined the page you posted and I confirmed that the solution:我检查了您发布的页面,并确认了解决方案:

$('selector').val('value').slider('refresh');

does indeed work.确实有效。 Make sure that 'selector' is referencing your select element, and that 'value' is a value you defined on the option element you wish to enable.确保“选择器”引用您的选择元素,并且“值”是您在要启用的选项元素上定义的值。

I confirmed this by visiting http://jquerymobile.com/demos/1.0.1/docs/forms/switch/index.html , then with firebug's console entering the line:我通过访问http://jquerymobile.com/demos/1.0.1/docs/forms/switch/index.html确认了这一点,然后使用 firebug 的控制台输入该行:

$("#flip-b").val('no').slider('refresh');

It switched the second slider displayed on the page from yes to no.它将页面上显示的第二个滑块从是到否。

There are two types of flipswitch , select based and checkbox based.两种类型的flipswitch ,基于选择和基于复选框 To set a select based widget, use the .val() construct要设置基于选择的小部件,请使用.val()构造

$("#myFlip").val("on").flipswitch( "refresh" ) ;

where "on" is one of the option values in the select element.其中“on”是 select 元素中的选项值之一。

To set a checkbox based widget, use the .prop() construct:要设置基于复选框的小部件,请使用.prop()构造:

$("#myFlip").prop( "checked", true ).flipswitch( "refresh" ) ;

where true is either true or false .其中truetruefalse (Thanks to Jeremy Hill for the hint). (感谢 Jeremy Hill 的提示)。

BTW I tried the .flipswitch( "option", "checked", true ) construct with no luck.顺便说一句,我尝试了.flipswitch( "option", "checked", true )构造,但没有运气。

Note there is also a slider switch similar to the select based flipswitch .请注意,还有一个类似于基于选择的flipswitch滑块开关。

For JQuery 1.4 or later, if you want to toggle a flip switch dynamically对于 JQuery 1.4 或更高版本,如果您想动态切换翻转开关

$("#flip").val('on').flipswitch('refresh');

Make sure that your data-role is flip-switch to make this work.确保您的数据角色翻转开关以使其工作。

Good Luck !祝你好运 !

Does this work:这是否有效:

JS JS

var fts = $('#flipMe');
fts.val('on');
fts.slider('refresh');

HTML HTML

<label for="flip-a">Select slider:</label>
<select name="slider" id="flipMe" data-role="slider">
    <option value="off">Off</option>
    <option value="on">On</option>
</select>

@Pizano's answer above worked for me. @Pizano 上面的回答对我有用。

I, too, was having this problem.我也遇到了这个问题。 In my case, I was missing the all-important ".slider('refresh')"就我而言,我错过了最重要的“.slider('refresh')”

Cheers, Steve干杯,史蒂夫

我无法让 'flipswitch' 部分工作,所以把它当作一个普通的复选框来处理可能对你有用(这适用于 jQuery Mobile 1.4.3):

$('input[name="my-flipswitch-name"]').prop('checked', true).trigger('click').trigger('change');

There are two ways to make flipswitches (checkbox-based and select-based), but you can can only dynamically change the state with the select-based method.有两种方法可以制作翻转开关(基于复选框和基于选择),但您只能使用基于选择的方法动态更改状态。

Jquery to change state: Jquery改变状态:

$("#mySwitch").val('no').flipswitch('refresh');

Select-based method:基于选择的方法:

<select name="mySwitch" id="mySwitch" data-role="flipswitch" >
   <option value="no">No</option>
   <option value="yes">Yes</option>
</select>

Checkbox-based: (can't change state dynamically)基于复选框:(不能动态改变状态)

<input type="checkbox" id="mySwitch" data-role="flipswitch">

$("#mySwitch").val('no').flipswitch().flipswitch('refresh'); seems to work, too.似乎也有效。

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

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