将变量值获取到 jQuery UI 调用中
我正在使用 HTML/CSS/JavaScript/jQuery 为 Sketchup 插件创建 UI 调色板。调色板中有许多实例需要带有关联滑块的输入框,因此我决定使用 JavaScript 创建一个“类”,以便可以将它们标记出来。输入框/滑块组合之一的 HTML 如下所示:
<span>H: <input type="text" class="InputBox" id="reg_panel_spacing_h"></span>
<div class="SliderSpan" id="reg_panel_spacing_h_slider"></div><br/>
JavaScript 的相关部分如下所示:
$(document).ready(function() {
function InputBoxJS (id, mi, ma, numtype, slider) {
this.current_val = mi;
this.element_id = id;
this.minimum = mi;
this.maximum = ma;
var self = this;
// string for selecting element using jquery
jq_id = "#" + this.element_id;
// slider code (if input box has associated slider)
if(slider = true) {
slider_jq_id = jq_id + "_slider" // id for selecting slider; assumes it's input id + "_slider"
$(slider_jq_id).slider({
min: mi,
max: ma,
value: self.current_val,
slide: function(event,ui) {
$(jq_id).val(ui.value); // PROBLEMATIC LINE
call_ruby("manipulate_data", self.element_id);
}
});
} // if slider = true
}
// create script objects for two input boxes
var reg_panel_spacing_h = new InputBoxJS("reg_panel_spacing_h", 500, 50000, 0, true);
var reg_panel_spacing_v = new InputBoxJS("reg_panel_spacing_v", 500, 50000, 0, true);
});
我遇到的问题是,当我使用此类创建两个输入框时(如上面的代码所示) ,两个滑块最终都与第二个输入框相关联。具体来说,上述代码中标记为有问题的 jq_id 变量最终对于两个输入框都被设置为“reg_panel_spacing_v”。奇怪的是,jq_id 在“问题线”上方的几行使用来设置 slider_jq_id,并在此处使用时按预期执行。
基本上似乎正在发生的事情是,包含“有问题的行”的函数在创建时与所有滑块的滑动事件相关联,并且它仅使用最近创建的输入框的 jq_id 。如果像这样更改“有问题的行”:
$(("#" + self.element_id)).val(ui.value);
问题就解决了。所以,我有一个解决方法。我只是希望有人能够向我解释到底发生了什么。
非常感谢!
乔什
I'm using the HTML/CSS/JavaScript/jQuery to create a UI palette for a Sketchup plug-in. There are numerous instances within the palette which require an input box with an associated slider, so I've decided to make a "class" using JavaScript so I can stamp them out. The HTML for one of the input box/slider combos looks like this:
<span>H: <input type="text" class="InputBox" id="reg_panel_spacing_h"></span>
<div class="SliderSpan" id="reg_panel_spacing_h_slider"></div><br/>
The relevant portion of the JavaScript looks like this:
$(document).ready(function() {
function InputBoxJS (id, mi, ma, numtype, slider) {
this.current_val = mi;
this.element_id = id;
this.minimum = mi;
this.maximum = ma;
var self = this;
// string for selecting element using jquery
jq_id = "#" + this.element_id;
// slider code (if input box has associated slider)
if(slider = true) {
slider_jq_id = jq_id + "_slider" // id for selecting slider; assumes it's input id + "_slider"
$(slider_jq_id).slider({
min: mi,
max: ma,
value: self.current_val,
slide: function(event,ui) {
$(jq_id).val(ui.value); // PROBLEMATIC LINE
call_ruby("manipulate_data", self.element_id);
}
});
} // if slider = true
}
// create script objects for two input boxes
var reg_panel_spacing_h = new InputBoxJS("reg_panel_spacing_h", 500, 50000, 0, true);
var reg_panel_spacing_v = new InputBoxJS("reg_panel_spacing_v", 500, 50000, 0, true);
});
The problem I'm having is that when I create two input boxes using this class (as shown in the above code), both sliders end up associated with the second input box. Specifically, the jq_id variable flagged in the above code as problematic ends up being set to "reg_panel_spacing_v" for both input boxes. What's bizarre is that jq_id is used a few lines above the "problematic line" to set slider_jq_id, and performs as expected when used there.
Basically what seems to be happening is that the function which contains the "problematic line" is getting associated with the slide event of ALL of the sliders when it's created, and it only uses the jq_id of the most recently-created input box. If change the "problematic line" like so:
$(("#" + self.element_id)).val(ui.value);
the problem is fixed. So, I have a workaround. I'm just hoping that someone would be able to explain to me exactly what's going on.
Many thanks!
Josh
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试将变量设置为本地变量,如下所示:
我怀疑如果不将其设置为本地变量,它就会获得解释“奇怪”行为的全局范围。
Try making the variable local, like this:
I suspect that without making it local it get global scope that explain the "bizarre" behaviour.