我想制作一个滑块来选择时间(从1:00到4:00),但它不起作用。它不与'select'通信。任何人都可以帮助我吗?
<script>
$(function() {
var select = $( "#czastrwania" );
var slider = $( "<div id='slider'></div>" ).insertAfter( select ).slider({
min: 60,
max: 240,
step: 30,
range: "min",
value: select[ 0 ].selectedIndex + 1,
slide: function( e, ui ) {
var hours = Math.floor(ui.value / 60);
var minutes = ui.value - (hours * 60);
if(hours.length == 1) hours = '0' + hours;
if(minutes.length == 1) minutes = '0' + minutes;
select[ 0 ].selectedIndex = ui.value - 1;
}
});
$( "#czastrwania" ).change(function() {
slider.slider( "value", this.selectedIndex + 1 );
});
});
</script>
<select id="czastrwania" name="czastrwania">
<option value="1:00">1:00</option>
<option value="1:30">1:30</option>
<option value="2:00">2:00</option>
<option value="2:30">2:30</option>
<option value="3:00">3:00</option>
<option value="3:30">3:30</option>
<option value="4:00">4:00</option>
</select>
答案 0 :(得分:1)
使用jQuery,你可以说
select.val('4:30')
不需要这个:
select[ 0 ].selectedIndex = ui.value - 1;
答案 1 :(得分:0)
现在它正在努力达到50%。如果我更改滑块,它会在选择时更改,但是当我更改选择值时,它不起作用。
<script>
$(function() {
var select = $( "#czastrwania" );
var slider = $( "<div id='slider'></div>" ).insertAfter( select ).slider({
min: 60,
max: 240,
step: 30,
range: "min",
value: select[ 0 ].selectedIndex + 1,
slide: function( e, ui ) {
var hours = Math.floor(ui.value / 60);
var minutes = ui.value - (hours * 60);
if(minutes == 0) minutes = '00';
select[ 0 ].value = hours+':'+minutes;
}
});
$( "#czastrwania" ).change(function() {
slider.slider( "value", select[ 0 ].value );
});
});
</script>
答案 2 :(得分:0)
解决。
<script>
$(function() {
var select = $( "#czastrwania" );
var slider = $( "<div id='slider'></div>" ).insertAfter( select ).slider({
min: 60,
max: 240,
step: 30,
range: "min",
value: select[ 0 ].selectedIndex + 1,
slide: function( e, ui ) {
var hours = Math.floor(ui.value / 60);
var minutes = ui.value - (hours * 60);
if(minutes == 0) minutes = '00';
select[ 0 ].value = hours+':'+minutes;
}
});
$( "#czastrwania" ).change(function() {
slider.slider( "value", 60 * select[ 0 ].value.replace(':','.') );
});
});
</script>