2010-05-04 27 views
1

我想從DIV元素的標題(這可能是問題)中讀取我的滑塊的值。這是推到以下內容:jQuery slider ui - 從HTML元素動態設置起始值

$(document).ready(function() { 
    $(".myslider5").slider({ 
     min: -1000, 
     max: 1000, 
     value: parseInt($(this).attr("title")), 
     slide: function(event, ui) { 
       // more actions 
     } 
    }); 

我想我得到NaN錯誤。任何線索?

+0

如何div的標題是怎樣的? – 2010-05-04 00:20:33

+0

'

-791
' – publicRavi 2010-05-04 00:26:12

+0

@publicRavi - 你是設置最小/最大?默認範圍是0-100,其中-791不會落入。 – 2010-05-04 00:37:25

回答

2

如果有超過這個類的一個,你需要在它們之間迭代,並使用.each()滑塊,就像這樣:

$(".myslider5").each(function() { 
    $(this).slider({ 
     value: parseInt($(this).attr("title")), 
     slide: function(event, ui) { 
       // more actions 
     }, 
     min: -1000, 
     max: 1000 
    }); 
}); 

Here's an example of the above,否則你會從標題中獲得的價值您可以看到.attr() documentation或詳細信息:

獲取匹配元素集中第一個元素的屬性值。

+0

@Nick C我試過了,但是保留了'$(「。myslider5」)。slider({'。謝謝:) – publicRavi 2010-05-04 01:36:42

0

這可能是$(this)沒有采取精確值,因此儘量給值:

$(".myslider5").each(function() { 
    $(this).slider({ 
     value: parseInt($(".DiVID").attr("title")), 
     slide: function(event, ui) { 
       // ... 
       // more actions 
       // ... 
     }, 
     min: -1000, 
     max: 1000 
    }); 
});