4

我只是想在这个 javascript 中添加代码以获取幻灯片的值以输入 type="hidden",但我无法将值输入 type="hidden",我做错了什么?无论如何,感谢您的帮助!

这是javascript代码:

// <![CDATA[
var hWidth, bWidth;

function setBgPos(v) {
    var off = v * hWidth;
    var pos = -bWidth + (v * bWidth);
    $('slider').setStyle({backgroundPosition: Math.round(pos - off) + 'px'});
}

function setSlideOutput(v) {
    $('percent').innerHTML = Math.round(v * 100) + '%';
}

Event.observe(window, 'load', function() {
    hWidth = $('slider-handle').getWidth();
    bWidth = $('slider-bar').getWidth();
    var slide = new Control.Slider('slider-handle', 'slider-bar', {
        sliderValue: 0.25,
        onSlide:
            function(v) {
                setBgPos(v);
                setSlideOutput(v);
                $('percentage').value = Math.round(v * 100) + '%';
            },
        onChange:
            function(v) {
                setBgPos(v);
                setSlideOutput(v);
                $('percentage').value = Math.round(v * 100) + '%';
            }
    });
    setBgPos(slide.value);
    setSlideOutput(slide.value);
});
// ]]>

html:

    <!DOCTYPE html>
    <html lang="nl">
    <head>
<script src="//ajax.googleapis.com/ajax/libs/prototype/1.7.1.0/prototype.js"></script>
<script src="//ajax.googleapis.com/ajax/libs/scriptaculous/1.9.0/scriptaculous.js"></script>
<style>
#slider, #slider-bar, #slider-handle {
    border: 0;
    padding: 0;
    margin: 0;
}
#slider {
    width: 300px;
    height:42px;
    background: url(img/bg.gif) no-repeat -300px;
    margin-left: 42px;
}
#slider-bar {
    width:300px;
    height:42px;
    background: url(img/track.png) no-repeat;
}
#slider-handle {
    width:42px;
    height:42px;
    cursor:move;
    background: url(img/gripper.png) no-repeat;
}
#percent {
    font-size: 75%;
    font-family: arial;
    font-weight: bold;
    text-align: center;
    padding-top: 1.1em;
}
</style>
    </head>
    <body>
    <div id="slider">
    <div id="slider-bar">
    <div id="slider-handle"><p id="percent"></p></div>
    </div>
    </div>
    <input type="hidden" id="percentage" name="percentage" value="0" />
    </body>
    </html>
4

3 回答 3

0

你应该使用$('#percentage')而不是$('percentage')

于 2013-06-01T18:06:12.307 回答
0

尝试使用 $('#percentage')[0].value

您需要的是 DOM 元素,而不是 jquery 对象。

或者,或者,使用$('#percentage').val('')

于 2013-06-01T18:38:45.090 回答
0

更新:解决添加设置值,这确实帮助了我:http ://www.ruby-forum.com/topic/143329 。感谢所有提示,更感谢 Geek Num 88!

于 2013-06-04T08:25:47.830 回答