我在页面上有一些不同行为的滑块。其中一个滑块与单选按钮组相关联。选中单选按钮时,滑块的值应更新。这很有效。但是,我无法反向工作 - 当手动移动滑块时,应检查相应值的单选按钮。
Here's a JSFiddle of the sliders.
这是我的JS:
//When radio buttons are changed, update slider - this works
$('[name="[smoker]"]').change(function () {
var value = this.value;
$('#smoker').slider("value", value);
});
//When slider is changed, update radio buttons - this doesn't work. The alert event never fires.
$('#smoker').change(function () {
alert(value)
($('[name="[smoker]"]').attr("value", value)).attr("checked",true);
});
这是我的加价:
<div class="slider" id="smoker" data-begin="0" data-end="4"></div>
<input type="radio" class="smoker_radio" name="[smoker]" value="0" id="smoker[1]">
<input type="radio" class="smoker_radio" name="[smoker]" value="1" id="smoker[2]">
<input type="radio" class="smoker_radio" name="[smoker]" value="2" id="smoker[3]">
<input type="radio" class="smoker_radio" name="[smoker]" value="3" id="smoker[4]">
<input type="radio" class="smoker_radio" name="[smoker]" value="4" id="smoker[5]">
答案 0 :(得分:2)
slide: function (event, ui) {
$(".smoker_radio[value='"+ui.value+"']").attr("checked", true);
//or better use .prop()
$(".smoker_radio[value='"+ui.value+"']").prop("checked", true);
}
更新了Fiddle
答案 1 :(得分:1)
尝试
//When slider is changed, update radio buttons
$('#smoker').on('slidechange', function () {
$('input[name="[smoker]"][value="' + $(this).slider('value') + '"]').prop("checked", true);
});
演示:Fiddle