我正在努力在一个单独的div中显示数据,当我传递我的javascript函数数据时,它不会触发onclick。它在我使用来自父级的数据时起作用,但在我将数据从父级转换为子级并尝试使用该数据传递JS函数之后不起作用。
##view##
@using WebApplication2.Models
@model IEnumerable<WebApplication2.Models.OBJECT>
@{
ViewBag.Title = "CompAndMon";
}
<script>
//this function does not fire when called
function setO(OfficeLocation,Name,Email,Phone,NumComputers,NumMonitors) {
var text = "Primary Contact Name " + Name+"\n Primary Contact Email: " +Email +"\n Primary Contact Phone: " +Phone +"\n Number of Computers: " +NumComputers +"\n Number Of Monitors: " +NumMonitors;
var location = "Office Location: " + OfficeLocation;
document.getElementById("Nametag").innerHTML = location;
return document.getElementById("OCM").innerHTML = text;
}
//this function does not fire when called
function setComputer(lastS) {
var text = "you selected Item No: " + lastS;
return document.getElementById("OCM").innerHTML = text;
}
//this function operates correctly
function setMonitor(id) {
var text = "you selected Item No: " + id;
return document.getElementById("OCM").innerHTML = text;
}
</script>
@foreach (var item in Model)
{
if (@item.Type == 1)
{
var office = item as Office;
var loc = @office.OfficeLocation;
var Name = @office.Name;
var email = @office.Email;
var phone = @office.Phone;
var mons = @office.NumMonitors;
var comps = @office.NumComputers;
<p><a onlick="setO(@loc,@Name,@email,@phone,@comps,@mons)">@office.Name</a></p>
}
else if (@item.Type == 2)
{
var computer = item as Computer;
<p>  <a onclick="setComputer(@computer.LastUser)">@item.Name1</a></p>
}
else
{
var monitor = item as Monitor;
<p>    <a onclick="setMonitor(@item.ID)">@item.Name1</a></p>
}
}
<h2 id="Nametag" style="text-align:center"></h2>
<div id ="OCM"class="row">
Select a computer or monitor and the information about the de will be displayed here.
</div>
以下是视图使用的类模型 ## OCM.cs ## 使用系统; 使用System.Collections.Generic; 使用System.ComponentModel.DataAnnotations; 使用System.Linq; 使用System.Web;
namespace WebApplication2.Models
{
public abstract class OBJECT
{
public int ID { get; set; }
public int Type { get; set; }
public string Name1 { get; set; }
}
public class Office:OBJECT
{
public string OfficeLocation { get; set; }
public string Name { get; set; }
public string Email { get; set; }
public string Phone { get; set; }
public int NumComputers { get; set; }
public int NumMonitors { get; set; }
}
public class Computer:OBJECT
{
public String LastUser { get; set; }
public String Name { get; set; }
public int NumMonitors { get; set; }
public String TotalHDSpace { get; set; }
public String FreeHDSpace { get; set; }
public int NumUpdates { get; set; }
}
public class Monitor:OBJECT
{
public String Manufacturer { get; set; }
public String ModelID { get; set; }
public String SerialNum { get; set; }
public int HoursON { get; set; }
public String LastTestTime { get; set; }
public String LastTestType { get; set; }
}
}
当我检查元素chrome时,它显示数据正在传递给函数,但是它没有运行所以我不知道该怎么做。任何帮助将不胜感激
Here shows that the data was passed to the JS functions when I inspect the element
答案 0 :(得分:0)
您使用不带引号的字符串 setComputer('ted')或setComputer(\“ted \”)而不是setComputer(ted)
这与电子邮件和其他参数相同
答案 1 :(得分:0)
我发现我在setO()调用中拼错了,需要将我的剃刀变量传递给单引号中的javascript函数。