我有一个表,表的最后一列有一个复选框,当用户单击一个复选框时,我想将行值传递给其他函数,但是当我这样做时,代码不会似乎在开枪,我想念什么吗?
function GetContractors(companyName) {
var workforce = companyName;
$.ajax({
type: 'GET',
url: '@Url.Action("getContractors","Permit")',
contentType: "application/json; charset=utf-8",
dataType: "json",
data: { "workforce": workforce },
success: function (result) {
$("#mainData").empty();
$.each(JSON.parse(result),
function (i, item) {
var row = i + 1;
$("#mainData").append(
"<tr>" +
"<td id='process_" + row + "'" + ">" + item.ContractorName + "</td>" +
"<td bgcolor= " + "'" + cellColour1 + "'" + " id='process_" + row + "'" + ">" + parseJsonDate(item.InductionExpiry) + "</td>" +
"<td bgcolor= " + "'" + cellColour + "'" + " id='process_" + row + "'" + ">" + parseJsonDate(item.InsuranceExpiry) + "</td>" +
"<td id='Action" + row + "'" + ">" +
"<checkbox id='checkbox' class='btn btn-xs btn-success " +
"'" + "onclick='SelectedUser(" + item.ContractorName + ")" +
"'" + ">" + "<i class='glyphicon glyphicon-trash" +
"'" + "></i>" + "</checkbox>" + " " +
"</tr>");
});
},
error: function (result) {
alert(result);
}
});
};
function SelectedUser(name)
{
alert(name);
}
答案 0 :(得分:2)
尝试一下:
"onclick='SelectedUser(\"" + item.ContractorName + "\")" +
您正在通过:
onclick='SelectedUser(Bob)'
应该有一个对象,但是应该是
onclick='SelectedUser("Bob")'
\"
转义双引号
答案 1 :(得分:1)
如果该值为字符串,则在撇号之间传递它,如下所示:
SelectedUser(" ' + item.ContractorName + ' ")
。