1

基本上,我有 2 个使用 Jquery 实现的滑块。I want to set the value in such a way that, when the first slider is selected say a range ex: 0-36. 我希望将第二个滑块的最大值动态设置为 36。这是代码。我无法将值设置为第二个滑块,请帮助。

$(function() {
$( "#slider-range-min" ).slider({
        range: "min",
        value: 0,
        min: 0,
        max: 100,
        slide: function( event, ui ) {
            $( "#greenData" ).val( "0 - " + ui.value );
        }
    });
    $( "#greenData" ).val( "$" + $( "#slider-range-min" ).slider( "value" ) );


});

    $(function() {
    $( "#slider-range-min-amber" ).slider({
            range: "min",
            value: 0,
            min: 0,
            max: 100,
            slide: function( event, ui ) {
                $( "#amberData" ).val( "0 - " + ui.value );
            }
        });
        $( "#amberData" ).val( "$" + $( "#slider-range-min-amber" ).slider( "value" ) );
    }); 

这是HTML代码:

<tr>
<td align="right"><label for="greenValue"
                            style="font-family: sans-serif; font-size: small;">
                                Green</label>
                                 </td>
                                 <td>   <div id="slider-range-min">  </div>  </td>
                                 <td> <input type="text" id="greenData" style="border: 0; color: green; font-weight: bold;" readonly="readonly" />  
                          </td>
                    </tr>

                    <tr>
                        <td align="right"><label
                            style="font-family: sans-serif; font-size: small;">
                                Amber</label>
                                 </td>
                                 <td id="amberTdId">    <div id="slider-range-min-amber">  </div>    </td>
                                 <td> <input type="text" id="amberData" style="border: 0; color: #FF7E00; font-weight: bold;" readonly="readonly" /> </td>
                    </tr>

当我从 0-36 选择绿色滑块的范围时,值 36 应设置为滑块 2。即..第二个滑块范围应为最小值:0 和最大值:36

我该怎么做。提前致谢。

4

2 回答 2

3

试试这个代码:

$(function() {
$( "#slider-range-min-amber" ).slider({
        range: "min",
        value: 0,
        min: 0,
        max: 100,
        slide: function( event, ui ) {
            $( "#amberData" ).val( "0 - " + ui.value );
        }
    });
    $( "#amberData" ).val( "$" + $( "#slider-range-min-amber" ).slider( "value" ) );
}); 

$(function() {
$( "#slider-range-min" ).slider({
    range: "min",
    value: 0,
    min: 0,
    max: 100,
    slide: function( event, ui ) {
        $( "#greenData" ).val( "0 - " + ui.value );
        var value = ui.value;
        var max = ui.value;
        $( "#slider-range-min-amber" ).slider("option","max",max).slider("value",value);
        $( "#amberData" ).val( "0 - " + ui.value );

    }
});
$( "#greenData" ).val( "$" + $( "#slider-range-min" ).slider( "value" ) );
});

或者看看我的小提琴:http: //jsfiddle.net/QPnxf/1/

于 2012-12-10T10:40:11.023 回答
2

简单的答案是在您的$('#greenData').val('0 - ' + ui.value);行之后添加以下内容:

$('#slider-range-min-amber').slider('option', 'max', ui.value);
$('#amberData').val('0 - ' + ui.value);

但是像这样重新查询 DOM 对性能不利。最好缓存您的 jQuery 对象并重新使用它们。一个更有效的解决方案是这样的:

$(function() {
  var green = $('#slider-range-min'),
      amber = $('#slider-range-min-amber'),
      greenData = $('#greenData'),
      amberData = $('#amberData'),
      opts = {
        range: 'min',
        value: 0,
        min: 0,
        max: 100
      };

  greenData.val('$' + opts.value);
  green.slider(opts).slider('option', 'slide', function(e, ui) {
    greenData.val(opts.min + ' - ' + ui.value);

    // this is the bit that updates the 
    // max value of the second slider
    amber.slider('option', 'max', ui.value);
    amberData.val(opts.min + ' - ' + ui.value);
  });

  amberData.val('$' + opts.value);
  amber.slider(opts).slider('option', 'slide', function(e, ui) {
    amberData.val(opts.min + ' - ' + ui.value);
  });
});
于 2012-12-10T10:44:16.220 回答