[英]Why do for loop executes before Jquery Selector in this code?
In the code below the for
loop executes always first, the jQuery selector comes later. 在
for
循环下面的代码中总是先执行,然后是jQuery选择器。 But I want to run this for loop after changing the value of the selected sliders. 但是我想在更改所选滑块的值后运行此for循环。
$('div[name="FirstSliderselector"]').slider('value', $("#MasterSlider").slider("value"));
var FirstSliders = $('div[name="FirstSliderselector"]');
for (var i = 0; i < FirstSliders.length; i++) {
console.log('for loop executed');
}
Edited to reflect new understanding of the question 编辑以反映对问题的新理解
What you want to do is pass a callback function for the change event, like this: 您要做的是为change事件传递一个回调函数,如下所示:
What I now understand is that you want to trigger a change event when you change the value of the slider. 我现在了解的是,您想在更改滑块的值时触发更改事件。 You set the value programmatically somewhere with:
您可以通过以下方式以编程方式设置该值:
$('div[name="FirstSliderselector"]').slider('value', $('#MasterSlider').slider('value'));
In order for your for
loop to execute after you've done this, you will need to have bound a change event to the slider, like this: 为了使
for
循环在执行完之后执行,您需要将一个change事件绑定到滑块,如下所示:
$('div[name="FirstSliderselector"]').slider({
change: function (event, ui) {
var firstSliders = $('div[name="FirstSliderselector"]'),
i;
for (i = 0; i < firstSliders.length; i += 1) {
console.log(ui.value);
}
}
});
You can also do it like this: 您也可以这样:
$('div[name="FirstSliderselector"]').on('slidechange', function (event, ui) {
var firstSliders = $('div[name="FirstSliderselector"]'),
i;
for (i = 0; i < firstSliders.length; i += 1) {
console.log(ui.value);
}
});
If you want to make sure this is only triggered when the value change is done programmatically and not after the slider slides, your callback function becomes this: 如果要确保仅在以编程方式完成值更改时才触发此操作,而不是在滑块滑动后才触发,则回调函数将变为:
function (event, ui) {
if (!event.originalEvent) {
var firstSliders = $('div[name="FirstSliderselector"]'),
i;
for (i = 0; i < firstSliders.length; i += 1) {
console.log(ui.value);
}
}
}
And finally, if none of these satisfy your needs, there is always, as suggested by Chris GW Green, the when-done construct: 最后,如果这些都不满足您的需求,那么就像克里斯·格林·格林(Chris GW Green)所建议的那样,总是有完成时的构造:
$.when($('div[name="FirstSliderselector"]').slider('value', $('#MasterSlider').slider('value'));).then(function () {
var firstSliders = $('div[name="FirstSliderselector"]'),
i;
for (i = 0; i < firstSliders.length; i += 1) {
console.log('for loop executed');
}
});
Guaranteed to run the for loop after the slider assignment... 保证在分配滑块后运行for循环...
var FirstSliders = $('div[name="FirstSliderselector"]'),
MastSliderValue = $("#MasterSlider").slider("value");
function updateSliders(){
FirstSliders.slider('value', MastSliderValue);
}
$.when(
updateSliders()
).done(
function(){
for (var i = 0; i < FirstSliders.length; i++) {
console.log('for loop executed');
}
}
);
Thanks for -1 on first attempt guys ;) new to this. 感谢-1在第一次尝试的家伙;)这是新的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.