0

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>

「スライダー」のクラスを持つ 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"
    });
});

これについて助けてくれてありがとう。

4

2 に答える 2

1

変化する

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);

于 2013-08-13T18:16:44.123 に答える
0
$(".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"
    });
});
于 2013-08-13T18:21:37.260 に答える