javascript中的函数未定义
作者:互联网
我在下面有这个js和qunit测试.为什么浏览器给我的listClasses没有定义?如何解决它.我看到的大多数都是
function ajax() {
$.ajax({
});}
但如果我确实喜欢下面怎么做测试?
$('#MregisteredClasses').on('pageinit', function listClasses(){
var rowInput = "1";
var pageInput = "1";
$.ajax({
url: 'http://137.57.102.146:8080/Training/getRegisteredClassesData.html',
data: ( {rows : rowInput , page : pageInput}),
type: 'POST',
success: function(json_results){
$('#list').append('<ul data-role="listview" data-inset="true"</ul>');
listItems = $('#list').find('ul');
$.each(json_results.rows, function(key) {
html = "<li data-mini='true' id='icon'><a href='http://137.57.102.146:8080/Training/MRegisteredClassesDetail.phone?courseId="
+ [json_results.rows[key].courseId] + "®No=" + [json_results.rows[key].regNo] +
"' rel='external'>" + json_results.rows[key].courseName+ "</a>"
+ "<a href='http://137.57.102.146:8080/Training/MRateCourse.phone?courseId="
+ [json_results.rows[key].courseId] + "®No=" + [json_results.rows[key].regNo] +
"' rel='external'>RATE THIS COURSE</a></li>" ;
listItems.append(html);
});
$('#list ul').listview();
},
});
});
这是qunit测试
test('asynchronous test', function() {
// Pause the test, and fail it if start() isn't called after one second
stop();
listClasses(function() {
// ...asynchronous assertions
ok(true, "Success");
});
setTimeout(function() {
start();
}, 2000);
});
解决方法:
$(‘#MregisteredClasses’).on(‘pageinit’,函数listClasses(){…使用不正确.
该函数要么是匿名的(即删除listClasses位),在这种情况下运行listClasses()将失败.
您需要做的是将函数声明外部化.
即:
function listClasses(){
var rowInput = "1";
var pageInput = "1";
$.ajax({
url: 'http://137.57.102.146:8080/Training/getRegisteredClassesData.html',
data: ( {rows : rowInput , page : pageInput}),
type: 'POST',
success: function(json_results){
$('#list').append('<ul data-role="listview" data-inset="true"</ul>');
listItems = $('#list').find('ul');
$.each(json_results.rows, function(key) {
html = "<li data-mini='true' id='icon'><a href='http://137.57.102.146:8080/Training/MRegisteredClassesDetail.phone?courseId="
+ [json_results.rows[key].courseId] + "®No=" + [json_results.rows[key].regNo] +
"' rel='external'>" + json_results.rows[key].courseName+ "</a>"
+ "<a href='http://137.57.102.146:8080/Training/MRateCourse.phone?courseId="
+ [json_results.rows[key].courseId] + "®No=" + [json_results.rows[key].regNo] +
"' rel='external'>RATE THIS COURSE</a></li>" ;
listItems.append(html);
});
$('#list ul').listview();
}
});
}
然后从on命令调用该函数:
$('#MregisteredClasses').on('pageinit', listClasses)
假设你的脚本只在listClasses()上失败,那么我会假设listClasses名称只是静默删除.
标签:javascript,function,ajax,qunit 来源: https://codeday.me/bug/20190709/1414696.html