【发布时间】:2015-07-04 23:45:20
【问题描述】:
我在一个不基于 Wordpress 的独立网站上使用 Jquery-option-tree 插件,如演示页面上的示例 7,除了我没有传递 .txt 文件而是 PHP 页面正在生成 传递给插件。
http://kotowicz.net/jquery-option-tree/demo/demo.html
这非常有效:假设用户想要为新产品选择一个类别,该插件适合在用户点击时生成一个漂亮的:“食物 -> 水果 -> 苹果”。 (参见演示页 ex. 7)
如果产品已经存在并分配了其类别,该怎么办?我想在用户编辑该产品并预加载树时将其展示给用户。
我有来自数据库的 ids 路径,因此只需使用我传递的值让插件在没有用户交互的情况下运行即可。我看到了这个问题:jQuery simulate click event on select option 并尝试使用这种(和其他)方法模拟用户的点击,但没有成功。
$('#select')
.val(value)
.trigger('click');
这里调用函数:
$(function() {
var options = {
empty_value: '',
set_value_on: 'each',
indexed: true, // the data in tree is indexed by values (ids), not by labels
on_each_change: '/js/jquery-option-tree/get-subtree.php', // this file will be called with 'id' parameter, JSON data must be returned
choose: function(level) {
return 'Choose level ' + level;
},
loading_image: '/js/jquery-option-tree/ajax-load.gif',
show_multiple: 10, // if true - will set the size to show all options
choose: ''
};
$.getJSON('/js/jquery-option-tree/get-subtree.php', function(tree) { // initialize the tree by loading the file first
$('input[name=parent_category_id]').optionTree(tree, options);
});
});
在这里你可以看到插件:
【问题讨论】:
-
不是试图找出你所有的问题,而是触发点击选择标签不是正常事件,通常你会触发
change。click毫无价值,因为它在用户选择过程中多次出现