jQuery 在初始化尝试调用方法“值”之前无法调用滑块上的方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17287521/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
cannot call methods on slider prior to initialization attempt to call method 'value'
提问by Kapil
I am using the following code for slider
我正在为滑块使用以下代码
Css and Jquery:
CSS 和 Jquery:
<link rel="stylesheet" href="http://code.jquery.com/ui/1.10.3/themes/smoothness/jquery-ui.css" />
<script type="text/javascript" src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script type="text/javascript" src="http://code.jquery.com/ui/1.10.3/jquery-ui.js"></script>
ASPX:
ASPX:
<p>
<label for="amount">Value:</label>
<input type="text" id="amount" style="border: 0; color: #f6931f; font-weight: bold;" />%
</p>
<div id="slider-vertical" style="height: 15px; width:800px"></div>
<script type="text/javascript">
$(function () {
$("#slider-vertical").slider({
orientation: "horizantal", range: "min", min: 0, max: 100, value: 60,
slide: function (event, ui)
{ $("#amount").val(ui.value); }
});
$("#amount").val($("#slider-vertical").slider("value"));
});
</script>
I am trying to set the value for slider:
我正在尝试设置滑块的值:
function UpdateSlider(PhaseInStatusReportVal) {
$("#slider-vertical").slider("value").val(PhaseInStatusReportVal);
}
I am calling above code from something like this :
我从这样的东西调用上面的代码:
ClientScript.RegisterStartupScript(Me.GetType(), "updatetheslider", "UpdateSlider('" + BCOMSPackageValidationList(0).PhaseInStatusReport.ToString() + "')", True)
but it's throwing the above error.
但它抛出了上述错误。
Can anyone please help me?
谁能帮帮我吗?
采纳答案by Dimitar Dimitrov
Have you considered a different approach. Setup a hidden field with the initial value -> populate the value from the code-behind, then do something like:
您是否考虑过不同的方法。使用初始值设置隐藏字段 -> 从代码隐藏中填充值,然后执行以下操作:
$(function () {
$("#slider-vertical").slider({
value: $("#hiddenFieldWhatever").val(),
// setup the rest ...
});
});
I hope I understood your requirements correct.
我希望我理解你的要求是正确的。
回答by Abdul Ahad
var slider = $("#slider-vertical");
var a = function() {
if(slider.slider("instance")) {
slider.slider('value', 1);
console.log('set value');
} else {
console.log('ERROR: cannot set value prior to initialization');
}
}
var b = function() {
slider.slider();
console.log('initialized');
}
a();
b();
a();
回答by mrbangybang
I found that if you use the event from the ON function from the slider library you can get that value. Ex:
我发现如果您使用滑块库中 ON 函数的事件,您可以获得该值。前任:
slider.on('slideStop', function(ev) {
let value= ev.value; //try ev if you want to see all
console.log(value);
})
Regards
问候
回答by Arun P Johny
In order to set the value of the slider you need to use the value method
为了设置滑块的值,您需要使用value 方法
$("#slider-vertical").slider("value", PhaseInStatusReportVal);
Demo: Fiddle
演示:小提琴
回答by devlife
So I have seen this type of thing a lot. You can do one of the following (AFAIK):
所以我经常看到这种类型的东西。您可以执行以下操作之一(AFAIK):
var slider;
$(function () {
var slider = $("#slider-vertical").slider({
orientation: "horizantal",
range: "min",
min: 0,
max: 100,
value: 60,
slide: function (event, ui) {
$("#amount").val(ui.value);
}
});
$("#amount").val(slider.slider("value"));
});
function UpdateSlider(PhaseInStatusReportVal) {
slider.slider("value").val(PhaseInStatusReportVal);
}
or
或者
$("#slider-vertical").slider().slider("value").val(PhaseInStatusReportVal);
I like the first option better.
我更喜欢第一个选项。