我试图实现一个滑块的视图形式(我已经隐藏了文本字段,并希望使一个脚本来显示滑块和更改/停止设置值为隐藏的文本字段。我的问题是事件根本就没有发生。我在模板的template.php中添加了jquery UI库和脚本(使用ZEN):
function ThemeName_preprocess_page(&$variables) {
drupal_add_library('system', 'ui');
drupal_add_library('system', 'ui.slider');
drupal_add_js('URL/js/search.js');
}
,这是javascript/jquery代码:
(function ($, Drupal, window, document, undefined) {
Drupal.behaviors.customSearch = {
attach: function(context, settings) {
$(document).ready(function (){
$("#distanta_slider").slider({
orientation: "horizontal",
range: "min",
max: 150,
min: 1,
value: 1,
//change: slider_change(),
//slide: slider_change(),
//stop: slider_change()
});
$("#distanta_slider").on("slidestop",slider_change());
$("#distanta_slider").on("slide",slider_change());
$("#distanta_slider").on("slidechange",slider_change());
});
function slider_change(){
alert($("#distanta_slider").slider("value"));
}
}
};
})(jQuery, Drupal, this, this.document);
我尝试使用$(selector)绑定事件。On,并试图在构造函数中声明它们(注释的3行)。在第一种情况下,在页面加载时,我收到了3个带有当前值的警报,在第二种情况下,我收到了3个警报,说"对象对象",但在这两种情况下,如果我移动滑块,绝对不会发生任何事情……没有错误,什么都没有。
任何帮助都是最感激的。提前谢谢你,
Cristi
在语句中不带参数调用函数名。
$("#distanta_slider").on("slidestop",slider_change);
$("#distanta_slider").on("slide",slider_change);
$("#distanta_slider").on("slidechange",slider_change);
错误的假设
$("#distanta_slider").on("slidestop",slider_change());
函数slider_change在这里立即执行