文档就绪变量未知
document ready var unknow
我使用 jquery、bootstrap 和把手。
当我点击一个标签时,我调用了一些函数。
在这些函数中,我使用了一些 var.
<script>
$(document).ready(function () {
var sourceLodgerContactAvailable = $("#lodger-contact-available-result-template").html();
var templateLodgerContactAvailable = Handlebars.compile(sourceLodgerContactAvailable);
$("a[href='#lodgerContactTab']").on('shown.bs.tab', function (e) {
e.target; // newly activated tab
e.relatedTarget; // previous active tabF
var location = assignLocationToLocal();
$.when(location).then(function () {
getNotAssociateContact();
getAssociateContact();
});
});
);
function getNotAssociateContact() {
$.ajax({
type: "GET",
url: "http://localhost:8080/rest/contacts/notassociatedto/" + $("#lodgerId").val(),
success: function (data, status, jqXHR) {
$("#lodgerContactAvailableDivTemplate").empty();
if (data.length != 0) {
$("#lodgerContactAvailableDivTemplate").append(templateLodgerContactAvailable(data)); //error
$('#lodgerContactAvailableTableResult').bootstrapTable('resetView');
}
},
error: function (jqXHR, status) {
check401Unauthorized(jqXHR);
}
});
}
</script>
我收到此错误消息。
templateLodgerContactAvailable 未定义
就好像函数中的var in unknow
我需要把函数放在文档里吗?
尝试从函数传递变量
$.when(location).then(function () {
getNotAssociateContact(templateLodgerContactAvailable);
getAssociateContact();
});
function getNotAssociateContact(templateLodgerContactAvailable) {
//your logic goes here
}
或者在document.ready函数外声明变量
变量被设置为它们的函数范围和任何子范围。
function aFunction() {
var a = 12;
}
function bFunction() {
// This won't work because `a` is defined in `aFunction`
console.log(a);
}
var c = 12;
function cFunction() {
// Works because `c` is declared outside of this function
console.log(c);
}
function dFunction() {
// Works for the same reason as `cFunction`
console.log(c);
}
因此,如果您想在这两个地方都使用该变量,您应该在 的 $(document).ready
之外声明它,然后在 $(document).ready
[] 的内部分配它的值=18=]
var templateLodgerContactAvailable;
$(document).ready(function() {
var sourceLodgerContactAvailable = $("#lodger-contact-available-result-template").html();
templateLodgerContactAvailable = Handlebars.compile(sourceLodgerContactAvailable);
...
});
一个更好的方法是让 getNotAssociateContact
接受一个参数。然后你可以将该变量传递给 getNotAssociateContact
.
function getNotAssociateContact(template) {
// Use `template` instead of `templateLodgerContactAvailable` inside of this function
...
}
然后当你想调用函数的时候,把值传给它就可以了。
var sourceLodgerContactAvailable = $("#lodger-contact-available-result-template").html();
var templateLodgerContactAvailable = Handlebars.compile(sourceLodgerContactAvailable);
$("a[href='#lodgerContactTab']").on('shown.bs.tab', function (e) {
e.target; // newly activated tab
e.relatedTarget; // previous active tabF
var location = assignLocationToLocal();
$.when(location).then(function() {
getNotAssociateContact(templateLodgerContactAvailable);
getAssociateContact();
});
});
我使用 jquery、bootstrap 和把手。 当我点击一个标签时,我调用了一些函数。 在这些函数中,我使用了一些 var.
<script>
$(document).ready(function () {
var sourceLodgerContactAvailable = $("#lodger-contact-available-result-template").html();
var templateLodgerContactAvailable = Handlebars.compile(sourceLodgerContactAvailable);
$("a[href='#lodgerContactTab']").on('shown.bs.tab', function (e) {
e.target; // newly activated tab
e.relatedTarget; // previous active tabF
var location = assignLocationToLocal();
$.when(location).then(function () {
getNotAssociateContact();
getAssociateContact();
});
});
);
function getNotAssociateContact() {
$.ajax({
type: "GET",
url: "http://localhost:8080/rest/contacts/notassociatedto/" + $("#lodgerId").val(),
success: function (data, status, jqXHR) {
$("#lodgerContactAvailableDivTemplate").empty();
if (data.length != 0) {
$("#lodgerContactAvailableDivTemplate").append(templateLodgerContactAvailable(data)); //error
$('#lodgerContactAvailableTableResult').bootstrapTable('resetView');
}
},
error: function (jqXHR, status) {
check401Unauthorized(jqXHR);
}
});
}
</script>
我收到此错误消息。
templateLodgerContactAvailable 未定义
就好像函数中的var in unknow
我需要把函数放在文档里吗?
尝试从函数传递变量
$.when(location).then(function () {
getNotAssociateContact(templateLodgerContactAvailable);
getAssociateContact();
});
function getNotAssociateContact(templateLodgerContactAvailable) {
//your logic goes here
}
或者在document.ready函数外声明变量
变量被设置为它们的函数范围和任何子范围。
function aFunction() {
var a = 12;
}
function bFunction() {
// This won't work because `a` is defined in `aFunction`
console.log(a);
}
var c = 12;
function cFunction() {
// Works because `c` is declared outside of this function
console.log(c);
}
function dFunction() {
// Works for the same reason as `cFunction`
console.log(c);
}
因此,如果您想在这两个地方都使用该变量,您应该在 的 $(document).ready
之外声明它,然后在 $(document).ready
[] 的内部分配它的值=18=]
var templateLodgerContactAvailable;
$(document).ready(function() {
var sourceLodgerContactAvailable = $("#lodger-contact-available-result-template").html();
templateLodgerContactAvailable = Handlebars.compile(sourceLodgerContactAvailable);
...
});
一个更好的方法是让 getNotAssociateContact
接受一个参数。然后你可以将该变量传递给 getNotAssociateContact
.
function getNotAssociateContact(template) {
// Use `template` instead of `templateLodgerContactAvailable` inside of this function
...
}
然后当你想调用函数的时候,把值传给它就可以了。
var sourceLodgerContactAvailable = $("#lodger-contact-available-result-template").html();
var templateLodgerContactAvailable = Handlebars.compile(sourceLodgerContactAvailable);
$("a[href='#lodgerContactTab']").on('shown.bs.tab', function (e) {
e.target; // newly activated tab
e.relatedTarget; // previous active tabF
var location = assignLocationToLocal();
$.when(location).then(function() {
getNotAssociateContact(templateLodgerContactAvailable);
getAssociateContact();
});
});