如何同步滑块?价值观?
我使用 jquery mobile sliders:
<div data-role="fieldcontain">
<input type="range" name="slider1" id="slider1" value="0" min="0" max="255" />
</div><br><br>
<div data-role="fieldcontain">
<select name="switcher1" id="switcher1" data-role="slider">
<option value="0">Off</option>
<option value="255">On</option>
</select>
</div>
它应该按以下方式工作:
- 如果触摸 switcher1,则 slider1 值应设置为 0 或 255;
- 如果 slider1 值发生更改,则 (a) 如果它 = 0,则 switcher1 应设置为 0,(b) 否则 switcher1 值应设置为 255。
我尝试使用 change 事件(同样用于
switcher1
change()
):
var slider1_val = "0";
$('#slider1').change(function () {
sVal = $(this).val();
if (slider1_val !== sVal) {
$("#slider1").val(sVal).slider("refresh");
if (sVal == 0) {
$("#switcher1").val(0).slider("refresh");
} else {
$("#switcher1").val(255).slider("refresh");
}
slider1_val = sVal;
}
});
但看起来每次调用 refresh
都会调用 change事件,所以我变得无限 环形。
I use jquery mobile sliders:
<div data-role="fieldcontain">
<input type="range" name="slider1" id="slider1" value="0" min="0" max="255" />
</div><br><br>
<div data-role="fieldcontain">
<select name="switcher1" id="switcher1" data-role="slider">
<option value="0">Off</option>
<option value="255">On</option>
</select>
</div>
It should work in the following way:
- if switcher1 is touched, then slider1 value should be set either to 0 or to 255;
- if slider1 value is changed, then (a) if it is = 0, then switcher1 should be set to 0, (b) else switcher1 value should be set to 255.
I've tried to do so with change
event (the same is used for switcher1
change()
):
var slider1_val = "0";
$('#slider1').change(function () {
sVal = $(this).val();
if (slider1_val !== sVal) {
$("#slider1").val(sVal).slider("refresh");
if (sVal == 0) {
$("#switcher1").val(0).slider("refresh");
} else {
$("#switcher1").val(255).slider("refresh");
}
slider1_val = sVal;
}
});
But looks like each call of refresh
calls change
event, so I am getting infinite loop.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
事实上,您有两个非常不同的标准来更改每个控件,这应该提示您更改事件处理程序也应该不同。使用相同的处理程序会导致您遇到的无限循环。下面的代码考虑了您提供的严格更改标准。请注意,slider1 更改处理程序仅在需要更改时更改 switcher1(根据您的条件),而不是每次调用它时。另请注意,在 slider1 更改处理程序中,在调用刷新之前设置
switcher1_val
,以便在 .slider('refresh')
< em>确实调用更改处理程序,更改处理程序不会执行任何操作,因为switcher1_val
已经更新。查看实例。
希望这会有所帮助。
更新:根据要求,该示例已被修改以使其更加通用。
The fact that you have two very different criteria for changing each control should tip you off that the change event handlers should be different as well. Using the same handlers leads to the infinite loop you are experiencing. The code below accounts for the strict change criteria you've provided. Note that the slider1 change handler changes switcher1 only if it needs to be changed (based on your criteria), not every time it is called. Also, note that in the slider1 change handler,
switcher1_val
is set before calling refresh, so that in case .slider('refresh')
does call the change handler, the change handler will not do anything, becauseswitcher1_val
is already updated.See the live example.
Hope this helps.
Update: As requested, the example has been modified to make it more general.