簡體   English   中英

jQuery UI滑塊填充值

[英]jquery ui slider populating the value

我正在使用jQuery ui設置一些滑塊。 我有很多滑塊,但是我正在嘗試最小化代碼。 這是我在html中設置滑塊的方式:

    <div class="budget_icon-wrapper">
        <div data-name="space_def" class="icon">
            <img class="info" src="images/info.png" />
            <img src="images/space-icon.png" />
        </div>
        <div id="space-item" class="space-item slider"></div>
        <div id="space_item-value" class="item-txt">$3.00</div>
    </div>

帶有“ slider”類的div是滑塊。 在它旁邊的div中,每個滑塊的值都不同,這恰好是3美元。 我想做的就是將值從該div中拉出(在本例中為3),並將其作為滑塊的值。

我嘗試過但不太有效的腳本是這樣的:

$(".slider").each(function() {
    value = parseInt($(this).siblings(".item-txt").text(),10);


    $(this).slider({
        animate: "fast",
        max: 25,
        min: 0,
        value: value,
        range: "min"
    });
});

感謝您對此的任何幫助。

更改

value = parseInt($(this).siblings(".item-txt").text(),10);

value = parseInt($(this).siblings(".item-txt").text().substring(1),10);

jsFiddle示例

parseInt返回NaN因為$導致它不返回您想要的內容。 正如parseInt上文檔所述 ,“如果第一個字符不能轉換為數字,則parseInt將返回NaN。”

更新:根據要求,如果您想要3.00的值,只需使用value = $(this).siblings(".item-txt").text().substring(1);

$(".slider").each(function() {
    strVal = $(this).next('.item-txt').text().substring(1); // take the substring of the text "$3.00" from the second character to the end (remove the $)
    value = parseInt(strVal,10);
    $(this).slider({
        animate: "fast",
        max: 25,
        min: 0,
        value: value,
        range: "min"
    });
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM