最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - jQuery UI slider: multiple sliders with default values - Stack Overflow

programmeradmin1浏览0评论

I have a page with multiple jQuery UI sliders on it. The sliders need to have a default value. For example, when somebody es to my form, sets the slider, submits the form but they get an error the sliders should not reset back to 0. All of the previous elements of my form that they filled out should remain in place. I know you can set a default value for the slider but I'm having trouble getting it to work with multiple sliders.

What I'm looking to do is take the value from the input field and use that as the default value for the range slider. You can see at the jsfiddle below I have the input's value's set to 2 and 4. When you load the page the sliders both go to the same position at 2.

Somehow I need to tell the slider to get the value of the input that is directly below it and use that as the default.

Any ideas on how to do this?

/

HTML:

<div class="kpa_rate kpa_rate1">
    <label for="kpa1_rating_value">Rating 1:</label>

    <div id="1" class="slider"></div>
    <input type="text" class="kpa1_rating_value" name="kpa1_rating" value="2" />       
</div>


<div class="kpa_rate kpa_rate2">
    <label for="kpa2_rating_value">Rating 2:</label>

    <div id="2" class="slider"></div>
    <input type="text" class="kpa2_rating_value" name="kpa2_rating" value="4" />
</div>

JS:

$(function() {
    $( ".slider" ).slider({
 range: "max",
 min: 0,
 max: 5,
 value: $("input", this).val(),
 slide: function( event, ui ) {                 
     //get the id of this slider
     var id = $(this).attr("id");
     //select the input box that has the same id as the slider within it and set it's value to the current slider value. 
         $("span[class*=" + id + "]").text(ui.value);
     $("input[class*=" + id + "]").val(ui.value);
 }
 });
});

I have a page with multiple jQuery UI sliders on it. The sliders need to have a default value. For example, when somebody es to my form, sets the slider, submits the form but they get an error the sliders should not reset back to 0. All of the previous elements of my form that they filled out should remain in place. I know you can set a default value for the slider but I'm having trouble getting it to work with multiple sliders.

What I'm looking to do is take the value from the input field and use that as the default value for the range slider. You can see at the jsfiddle below I have the input's value's set to 2 and 4. When you load the page the sliders both go to the same position at 2.

Somehow I need to tell the slider to get the value of the input that is directly below it and use that as the default.

Any ideas on how to do this?

http://jsfiddle/dmcgrew/EquTn/3/

HTML:

<div class="kpa_rate kpa_rate1">
    <label for="kpa1_rating_value">Rating 1:</label>

    <div id="1" class="slider"></div>
    <input type="text" class="kpa1_rating_value" name="kpa1_rating" value="2" />       
</div>


<div class="kpa_rate kpa_rate2">
    <label for="kpa2_rating_value">Rating 2:</label>

    <div id="2" class="slider"></div>
    <input type="text" class="kpa2_rating_value" name="kpa2_rating" value="4" />
</div>

JS:

$(function() {
    $( ".slider" ).slider({
 range: "max",
 min: 0,
 max: 5,
 value: $("input", this).val(),
 slide: function( event, ui ) {                 
     //get the id of this slider
     var id = $(this).attr("id");
     //select the input box that has the same id as the slider within it and set it's value to the current slider value. 
         $("span[class*=" + id + "]").text(ui.value);
     $("input[class*=" + id + "]").val(ui.value);
 }
 });
});
Share Improve this question asked Jun 24, 2013 at 14:25 DustinDustin 4,45912 gold badges56 silver badges96 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 7

Replace your value option with the create event:

create: function () {
    $(this).slider( "option", "value", $(this).next().val() );
},

jsFiddle example

Best answer from jQueryUI itself multiple sliders

<div id="eq">
  <span>88</span>
  <span>77</span>
  <span>55</span>
 </div>

// Script
$( "#eq > span" ).each(function() {

  // read initial values from markup and remove that
  var value = parseInt( $( this ).text(), 10 );

  $( this ).empty().slider({
    value: value,
    range: "min",
    animate: true,
    orientation: "vertical"
  });
});
发布评论

评论列表(0)

  1. 暂无评论
ok 不同模板 switch ($forum['model']) { /*case '0': include _include(APP_PATH . 'view/htm/read.htm'); break;*/ default: include _include(theme_load('read', $fid)); break; } } break; case '10': // 主题外链 / thread external link http_location(htmlspecialchars_decode(trim($thread['description']))); break; case '11': // 单页 / single page $attachlist = array(); $imagelist = array(); $thread['filelist'] = array(); $threadlist = NULL; $thread['files'] > 0 and list($attachlist, $imagelist, $thread['filelist']) = well_attach_find_by_tid($tid); $data = data_read_cache($tid); empty($data) and message(-1, lang('data_malformation')); $tidlist = $forum['threads'] ? page_find_by_fid($fid, $page, $pagesize) : NULL; if ($tidlist) { $tidarr = arrlist_values($tidlist, 'tid'); $threadlist = well_thread_find($tidarr, $pagesize); // 按之前tidlist排序 $threadlist = array2_sort_key($threadlist, $tidlist, 'tid'); } $allowpost = forum_access_user($fid, $gid, 'allowpost'); $allowupdate = forum_access_mod($fid, $gid, 'allowupdate'); $allowdelete = forum_access_mod($fid, $gid, 'allowdelete'); $access = array('allowpost' => $allowpost, 'allowupdate' => $allowupdate, 'allowdelete' => $allowdelete); $header['title'] = $thread['subject']; $header['mobile_link'] = $thread['url']; $header['keywords'] = $thread['keyword'] ? $thread['keyword'] : $thread['subject']; $header['description'] = $thread['description'] ? $thread['description'] : $thread['brief']; $_SESSION['fid'] = $fid; if ($ajax) { empty($conf['api_on']) and message(0, lang('closed')); $apilist['header'] = $header; $apilist['extra'] = $extra; $apilist['access'] = $access; $apilist['thread'] = well_thread_safe_info($thread); $apilist['thread_data'] = $data; $apilist['forum'] = $forum; $apilist['imagelist'] = $imagelist; $apilist['filelist'] = $thread['filelist']; $apilist['threadlist'] = $threadlist; message(0, $apilist); } else { include _include(theme_load('single_page', $fid)); } break; default: message(-1, lang('data_malformation')); break; } ?>