I've looked around for a solution but I might just be missing something really obvious because they don't solve my issue. I am no JS wiz at all, just a disclaimer.
我一直在寻找解决方案,但我可能只是错过了一些非常明显的东西,因为它们没有解决我的问题。我根本不是JS,只是一个免责声明。
I have an ASP project where JavaScript calls some C# code some times. I start my code with this:
我有一个ASP项目,其中JavaScript有时会调用一些C#代码。我用这个开始我的代码:
window.onload = function () {
LiveSearch();
getCredentials();
getAllUsers();
getIsAdmin();
};
All of these functions work just fine. But the one of interest is getAllUsers() because it contacts the backend via an AJAX call to get some data to fill in a table.
所有这些功能都可以正常工作。但感兴趣的是getAllUsers(),因为它通过AJAX调用联系后端以获取一些数据来填充表。
function getAllUsers() {
var result_body = "";
$.ajax({
type: 'GET',
url: '/Home/GetAllUsers',
dataType: 'json',
cache: false,
contentType: 'application/json; charset=utf-8',
data: JSON.stringify(""),
success: function (users) {
PushToScope("users", users);
var dict = scope[2];
if (dict.key.length > 0) {
for (var key in dict.value) {
result_body += '<tr onclick="getClickedUserObject(' + dict["value"][key].Initials + ')\">';
result_body += '<td class=\"col-xs-4\">' + dict["value"][key].Name + '</td>'
result_body += '<td class=\"col-xs-4\">' + dict["value"][key].Title + '</td>'
result_body += '<td class=\"col-xs-4\">' + dict["value"][key].Department + '</td>'
result_body += '<td style=\"display: none\">' + dict["value"][key].PrivatePhone + '</td>'
result_body += '<td style=\"display: none\">' + dict["value"][key].WorkEmail + '</td>'
result_body += '<td style=\"display: none\">' + dict["value"][key].WorkPhoneLandline + '</td>'
result_body += '<td style=\"display: none\">' + dict["value"][key].WorkPhoneMobile + '</td>'
result_body += '</tr>';
}
} else {
result_body += '<tr>';
result_body += '<td style=\"col-xs-12\"><b>No Data. Try again, or Contact IT Support.</b></td>';
result_body += '</tr>';
}
$('#result-table').html(result_body);
}
});
}
Like I said, the above works, but the problem comes forth when I click an element in my table. "getClickedUserObject()" below:
就像我说的,上面的工作,但当我点击我的表中的元素时,问题出现了。下面的“getClickedUserObject()”:
function getClickedUserObject(lettercode) {
if (lettercode != undefined) {
var users = scope[2];
var user = users["value"][lettercode];
$('#result-title').html(user.Title);
$('#result-name').html(user.Name);
$('#result-department').html(user.Department);
$('#result-email').html('<a href=\"mailto:' + user.WorkEmail + '\">' + work.WorkEmail + '</a>');
$('#result-work-mobile').html(user.WorkPhoneMobile);
$('#result-work-landline').html(user.WorkPhoneLandline);
$('#result-private-mobile').html(user.PrivatePhone);
if (lettercode == scope[0]) {
$("#HidePrivate").show();
$("#HidePrivate").disabled = false;
$("#HidePrivate").checked = user.HiddenPrivatePhone;
} else {
$("#HidePrivate").hide();
$("#HidePrivate").disabled = true;
}
}
return false;
}
This function never fires, instead I get the error in the title, saying that whatever lettercode I would get from clicking a row is not defined. This is odd to me because looking in the Google Chrome inspector I see this:
这个函数永远不会触发,而是我在标题中得到错误,说没有定义单击行所得到的任何字母代码。这对我来说很奇怪,因为查看Google Chrome检查器我看到了:
So what gives?
什么赋予了什么?
1 个解决方案
#1
2
I'm not familiar with your function, but maybe the argument should be a string? Looks like you don't have any quotes around it in the function call.
我不熟悉你的功能,但也许参数应该是一个字符串?看起来你在函数调用中没有任何引号。
Like so:
result_body += '<tr onclick=\"getClickedUserObject(\'' + dict["value"][key].Initials + '\')\">';
#1
2
I'm not familiar with your function, but maybe the argument should be a string? Looks like you don't have any quotes around it in the function call.
我不熟悉你的功能,但也许参数应该是一个字符串?看起来你在函数调用中没有任何引号。
Like so:
result_body += '<tr onclick=\"getClickedUserObject(\'' + dict["value"][key].Initials + '\')\">';