【发布时间】:2015-12-19 20:21:42
【问题描述】:
我试图在按钮单击时获取动态生成的
编辑:更新代码
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.js">
</script>
</head>
<body>
<script>
$(document).ready(function() {
var placeholder = $("#test");
var yearList = $("<select />", {'class': 'form-sth', 'id': 'year-list'});
var weekList = $("<select />", {'class': 'form-sth', 'id': 'week-list'});
var fileList = $("<select />", {'class': 'form-sth', 'id': 'file-list'});
var viewButton = $("<button />", {'class': 'btn btn-default', 'id': 'view', 'text': 'view'});
function populate(list, base_dir, prefix, postfix, bFiles) {
$.ajax({
url: 'stat.php',
type: 'GET',
data: { dir: base_dir, files: (bFiles == true) ? '1' : '0' },
dataType: 'json',
success: function(json) {
json.content.forEach(function(entry) {
var option = $("<option />", {'value': prefix + entry + postfix, 'text': entry} );
list.append(option);
});
var lastOption = list.children('option:last-child');
lastOption.attr('selected', 'selected');
lastOption.trigger('change');
}
})
}
var baseDir = '/var/www/_cronjob/data/ups-stats/';
populate(yearList, baseDir, '', '/', false);
yearList.on('change', function() {
console.log('#year-list : onChange()');
var selectedOption = $('#year-list option:selected');
populate(weekList, baseDir + selectedOption.attr('value'), selectedOption.attr('value'), '/', false);
})
weekList.on('change', function() {
console.log('#week-list : onChange()');
var selectedOption = $('#week-list option:selected');
populate(fileList, baseDir + selectedOption.attr('value'), selectedOption.attr('value'), '', true);
})
var delay = 1500;
yearList.appendTo(placeholder).hide().fadeIn(delay);
weekList.appendTo(placeholder).hide().fadeIn(delay);
fileList.appendTo(placeholder).hide().fadeIn(delay);
viewButton.appendTo(placeholder).hide().fadeIn(delay);
viewButton.on('click', function() {
console.log('#view : onClick()');
var option = $('#file-list option:selected');
console.log(baseDir + option.attr('value'));
})
console.log('button click! ...');
$("#view").trigger("click");
});
</script>
<div id="test">
</div>
</body>
</html>
stat.php?dir=/var/www/&files=1 的示例输出
{"content":["index.php","test.php"]}
我不明白这一点,谁能解释一下为什么会发生这种情况以及如何解决这个问题?
编辑:我已经放置了许多 console.log() 函数来跟踪正在发生的事情,并且我发现生成
详情: 我有 3 个 列表。第一个包含年份文件夹,第二个:周文件夹,第三个:文件名。一开始我正在加载“年份”文件夹,然后它类似于链反应:每个 的 onChange() 函数正在加载下一个
的数据【问题讨论】:
-
你试过“viewButton.click().trigger()”
-
viewButton.click().trigger(); - 未捕获的类型错误:无法将 undefined 或 null 转换为对象
标签: jquery select button dynamic click