【发布时间】:2011-08-31 17:34:19
【问题描述】:
编辑:http://jsfiddle.net/vol7ron/wQZdM/
小提琴应该用来帮助说明我正在尝试做的事情以及正在发生的事情。应使用第二个选项值填充子选择。
原问题:
不确定最好的提问方式。我正在创建一个测试脚本来自动填充表单上的输入。
它包括一系列select 下拉框,它们在 onChange 事件中填充其他 select 选项。尝试自动填充表单时,子选择没有任何选项。
console.clear();
// non-select inputs
$(':input:not([type="hidden"],[type="button"])').each(function(){
$(this).val($(this).attr('name')) // test value is simple input's name
});
// select inputs
var count=0, cutoff=7500;
$('select').each(function(){
var t = $(this);
var c = t.children('option');
while( c.length <= 1 && count < cutoff){
count++;
c = $(this).children('option'); // tried not using the cache'd variable
if (!(count % 10))
console.log(count, c.length, "No Options"); // debugging -- never exists early
setTimeout(function(){},0); // not really doing anything
}
t.val( c.eq(1).val() ); // set value to second option value
t.trigger('change'); // calls the onChange even if it doesnt exist
});
// verify it does have data
console.log($('#sub-select').children('option').length); // does have options
更改事件中有一个 AJAX 调用。我可以修改回调,但这只是一个简单的测试集脚本,从控制台运行。有什么想法吗?
【问题讨论】:
-
您的第一个选择器相当多。可以简化为:
input:not([type="hidden"],:button). -
从代码和解释看问题不是很清楚。
-
您应该包含纯 Javascript 或添加 jQuery 标签。大约 80% 的 Javascript 开发人员不会处理 jQuery 问题。但请记住,大约 90% 的互联网 cmet 统计数据都是即时生成的。
-
嗯,一种提问方式是实际提出问题......很难说出您可能遇到什么问题,因为大多数代码似乎根本没有真正的目的。跨度>
-
@pimvdb:包括我不想要的按钮。我确实有一个错字(应该两次提交而不是按钮)。
标签: javascript jquery testing